mvpa2.clfs.knn.kNN

Inheritance diagram of kNN

class mvpa2.clfs.knn.kNN(k=2, dfx=<function squared_euclidean_distance>, voting='weighted', **kwargs)

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 actual training is performed as the training dataset is simply stored in the classifier. All computations are done during classifier prediction.

Notes

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

Available conditional attributes:

  • calling_time+: None
  • distances: Distances computed for each sample
  • estimates+: Internal classifier estimates the most recent predictions are based on
  • predicting_time+: Time (in seconds) which took classifier to predict
  • predictions+: Most recent set of predictions
  • raw_results: None
  • trained_dataset: None
  • trained_nsamples+: None
  • trained_targets+: None
  • training_stats: Confusion matrix of learning performance
  • training_time+: None

(Conditional attributes enabled by default suffixed with +)

Methods

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_ca : None or list of str

Names of the conditional attributes which should be enabled in addition to the default ones

disable_ca : None or list of str

Names of the conditional attributes which should be disabled

auto_train : bool

Flag whether the learner will automatically train itself on the input dataset when called untrained.

force_train : bool

Flag whether the learner will enforce training on the input dataset upon every call.

space : str, optional

Name of the ‘processing space’. The actual meaning of this argument heavily depends on the sub-class implementation. In general, this is a trigger that tells the node to compute and store information about the input data that is “interesting” in the context of the corresponding processing in the output dataset.

pass_attr : str, list of str|tuple, optional

Additional attributes to pass on to an output dataset. Attributes can be taken from all three attribute collections of an input dataset (sa, fa, a – see Dataset.get_attr()), or from the collection of conditional attributes (ca) of a node instance. Corresponding collection name prefixes should be used to identify attributes, e.g. ‘ca.null_prob’ for the conditional attribute ‘null_prob’, or ‘fa.stats’ for the feature attribute stats. In addition to a plain attribute identifier it is possible to use a tuple to trigger more complex operations. The first tuple element is the attribute identifier, as described before. The second element is the name of the target attribute collection (sa, fa, or a). The third element is the axis number of a multidimensional array that shall be swapped with the current first axis. The fourth element is a new name that shall be used for an attribute in the output dataset. Example: (‘ca.null_prob’, ‘fa’, 1, ‘pvalues’) will take the conditional attribute ‘null_prob’ and store it as a feature attribute ‘pvalues’, while swapping the first and second axes. Simplified instructions can be given by leaving out consecutive tuple elements starting from the end.

postproc : Node instance, optional

Node to perform post-processing of results. This node is applied in __call__() to perform a final processing step on the to be result dataset. If None, nothing is done.

descr : str

Description of the instance

Methods

dfx