Package Bio :: Module kNN
[hide private]
[frames] | no frames]

Module kNN

source code

This module provides code for doing k-nearest-neighbors classification.

k Nearest Neighbors is a supervised learning algorithm that classifies a new observation based the classes in its surrounding neighborhood.

Glossary:

Classes:

Functions:

Weighting Functions:

Classes [hide private]
  kNN
Holds information necessary to do nearest neighbors classification.
Functions [hide private]
1
equal_weight(x, y) source code
kNN
train(xs, ys, k)
Train a k nearest neighbors classifier on a training set.
source code
 
calculate(knn, x, weight_fn=<function equal_weight at 0x2eff140>, distance_fn=None)
calculate(knn, x[, weight_fn][, distance_fn]) -> weight dict
source code
 
classify(knn, x, weight_fn=<function equal_weight at 0x2eff140>, distance_fn=None)
classify(knn, x[, weight_fn][, distance_fn]) -> class
source code
Variables [hide private]
  __package__ = 'Bio'
Function Details [hide private]

train(xs, ys, k)

source code 
Train a k nearest neighbors classifier on a training set. xs is a list of observations and ys is a list of the class assignments. Thus, xs and ys should contain the same number of elements. k is the number of neighbors that should be examined when doing the classification.
Returns: kNN

calculate(knn, x, weight_fn=<function equal_weight at 0x2eff140>, distance_fn=None)

source code 

calculate(knn, x[, weight_fn][, distance_fn]) -> weight dict

Calculate the probability for each class. knn is a kNN object. x is the observed data. weight_fn is an optional function that takes x and a training example, and returns a weight. distance_fn is an optional function that takes two points and returns the distance between them. If distance_fn is None (the default), the Euclidean distance is used. Returns a dictionary of the class to the weight given to the class.

classify(knn, x, weight_fn=<function equal_weight at 0x2eff140>, distance_fn=None)

source code 

classify(knn, x[, weight_fn][, distance_fn]) -> class

Classify an observation into a class. If not specified, weight_fn will give all neighbors equal weight. distance_fn is an optional function that takes two points and returns the distance between them. If distance_fn is None (the default), the Euclidean distance is used.