| Multivariate Pattern Analysis in Python

### Navigation

- index
- next |
- previous |
- ExpPsy Project |
- NeuroDebian Project |
- PyMVPA 0.4.x Home |
- PyMVPA Home |
- Documentation »
- Module Reference »

This content refers to the previous stable release of PyMVPA. Please visit www.pymvpa.org for the most recent version of PyMVPA and its documentation.

# clfs.knn

## Module: `clfs.knn`

Inheritance diagram for `mvpa.clfs.knn`:

k-Nearest-Neighbour classifier.

## `kNN`

class `mvpa.clfs.knn.``kNN`(k=2, dfx=<function squared_euclidean_distance at 0xb069064>, voting='weighted', **kwargs)

  Bases: `mvpa.clfs.base.Classifier`

  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.

  Note

  If enabled, kNN stores the votes per class in the ‘values’ state after calling predict().

  Note

  Available state variables:

  - feature_ids: Feature IDS which were used for the actual training.
  - predicting_time+: Time (in seconds) which took classifier to predict
  - predictions+: Most recent set of predictions
  - trained_dataset: The dataset it has been trained on
  - trained_labels+: Set of unique labels it has been trained on
  - trained_nsamples+: Number of samples it has been trained on
  - training_confusion: Confusion matrix of learning performance
  - training_time+: Time (in seconds) which took classifier to train
  - values+: Internal classifier values the most recent predictions are based on

  (States enabled by default are listed with +)

  See also

  Please refer to the documentation of the base class for more information:

  `Classifier`

  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).
  - enable_states (None or list of basestring) – Names of the state variables which should be enabled additionally to default ones
  - disable_states (None or list of basestring) – Names of the state variables which should be disabled

  `getMajorityVote`(knn_ids)

    Simple voting by choosing the majority of class neighbors.

  `getWeightedVote`(knn_ids)

    Vote with classes weighted by the number of samples per class.

  `untrain`()

    Reset trained state

### Navigation

- index
- next |
- previous |
- ExpPsy Project |
- NeuroDebian Project |
- PyMVPA 0.4.x Home |
- PyMVPA Home |
- Documentation »
- Module Reference »