Package mvpa :: Package clfs :: Module knn :: Class kNN
[hide private]
[frames] | no frames]

Class kNN

source code


k-Nearest-Neighbour classifier.

This is a simple classifier that bases its decision on the distances between the training dataset samples and the test sample(s). Distances are computed using a customizable distance function. A certain number (k)of nearest neighbors is selected based on the smallest distances and the labels of this neighboring samples are fed into a voting function to determine the labels of the test sample.

Training a kNN classifier is extremely quick, as no actuall training is performed as the training dataset is simply stored in the classifier. All computations are done during classifier prediction.

If enabled, kNN stores the votes per class in the 'values' state after calling predict().
Nested Classes [hide private]

Inherited from misc.state.ClassWithCollections: __metaclass__

Instance Methods [hide private]
 
__init__(self, k=2, dfx=squared_euclidean_distance, voting='weighted', **kwargs)
Cheap initialization.
source code
 
__repr__(self, prefixes=[])
Representation of the object
source code
 
__str__(self)
str(x)
source code
 
_train(self, data)
Train the classifier.
source code
 
_predict(self, data)
Predict the class labels for the provided data.
source code
 
getMajorityVote(self, knn_ids)
Simple voting by choosing the majority of class neighbors.
source code
 
getWeightedVote(self, knn_ids)
Vote with classes weighted by the number of samples per class.
source code
 
untrain(self)
Reset trained state
source code

Inherited from base.Classifier: clone, getSensitivityAnalyzer, isTrained, predict, repredict, retrain, summary, train, trained

Inherited from base.Classifier (private): _getFeatureIds, _postpredict, _posttrain, _prepredict, _pretrain, _regressionIsBogus, _setRetrainable

Inherited from misc.state.ClassWithCollections: __getattribute__, __new__, __setattr__, reset

Inherited from object: __delattr__, __format__, __hash__, __reduce__, __reduce_ex__, __sizeof__, __subclasshook__

Class Variables [hide private]
  _clf_internals = ['knn', 'non-linear', 'binary', 'multiclass',...
Describes some specifics about the classifier -- is that it is doing regression for instance....

Inherited from base.Classifier: _DEV__doc__, feature_ids, predicting_time, predictions, regression, retrainable, trained_dataset, trained_labels, trained_nsamples, training_confusion, training_time, values

Inherited from misc.state.ClassWithCollections: descr

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, k=2, dfx=squared_euclidean_distance, voting='weighted', **kwargs)
(Constructor)

source code 
Cheap initialization.
Overrides: object.__init__

Parameters:

k: unsigned integer
Number of nearest neighbours to be used for voting.
dfx: functor
Function to compute the distances between training and test samples. Default: squared euclidean distance
voting: str
Voting method used to derive predictions from the nearest neighbors. Possible values are 'majority' (simple majority of classes determines vote) and 'weighted' (votes are weighted according to the relative frequencies of each class in the training data).
**kwargs:
Additonal arguments are passed to the base class.

__repr__(self, prefixes=[])
(Representation operator)

source code 
Representation of the object
Parameters:
  • fullname - Either to include full name of the module
  • prefixes - What other prefixes to prepend to list of arguments
Overrides: object.__repr__

__str__(self)
(Informal representation operator)

source code 
str(x)
Overrides: object.__str__
(inherited documentation)

_train(self, data)

source code 

Train the classifier.

For kNN it is degenerate -- just stores the data.

Overrides: base.Classifier._train

_predict(self, data)

source code 

Predict the class labels for the provided data.

Returns a list of class labels (one for each data sample).

Overrides: base.Classifier._predict

untrain(self)

source code 
Reset trained state
Overrides: base.Classifier.untrain

Class Variable Details [hide private]

_clf_internals

Describes some specifics about the classifier -- is that it is doing regression for instance....
Value:
['knn', 'non-linear', 'binary', 'multiclass', 'notrain2predict']