Package PyML :: Package classifiers :: Module knn :: Class KNN
[frames] | no frames]

Class KNN

source code

base.pymlObject.PyMLobject --+    
                             |    
    baseClassifiers.Classifier --+
                                 |
                                KNN

a K-Nearest-Neighbors classifier

:Keywords:

if the training data is a C++ dataset (e.g. SparseDataSet) classification is much faster since everything is done in C++; if a python container is used then it's a slower pure python implementation.

Nested Classes
    Inherited from baseClassifiers.Classifier
  resultsObject
Instance Methods
 
__init__(self, arg=None, **args) source code
 
__repr__(self) source code
 
train(self, data, **args) source code
 
classify(self, data, i)
For each class the sum of the distances to the k nearest neighbors is computed.
source code
 
test(self, data, **args)
test a classifier on a given dataset
source code
 
testC(self, data, **args) source code
 
nearestNeighbor(self, data, pattern) source code
    Inherited from baseClassifiers.Classifier
 
cv(classifier, data, numFolds=5, **args)
perform k-fold cross validation
source code
 
getTrainingTime(self) source code
 
logger(self) source code
 
loo(classifier, data, **args)
perform Leave One Out
source code
 
nCV(classifier, data, **args)
runs CV n times, returning a 'ResultsList' object.
source code
 
project(self, data)
project a test dataset to the training data features.
source code
 
save(self, fileHandle) source code
 
stratifiedCV(classifier, data, numFolds=5, **args)
perform k-fold stratified cross-validation; in each fold the number of patterns from each class is proportional to the relative fraction of the class in the dataset
source code
 
trainFinalize(self) source code
 
trainTest(classifierTemplate, data, trainingPatterns, testingPatterns, **args)
Train a classifier on the list of training patterns, and test it on the test patterns
source code
 
twoClassClassify(self, data, i) source code
Class Variables
  attributes = {'k': 3}
    Inherited from baseClassifiers.Classifier
  deepcopy = False
  type = 'classifier'
Method Details

__init__(self, arg=None, **args)
(Constructor)

source code 
Overrides: baseClassifiers.Classifier.__init__

__repr__(self)
(Representation operator)

source code 
Overrides: baseClassifiers.Classifier.__repr__

train(self, data, **args)

source code 
Overrides: baseClassifiers.Classifier.train

classify(self, data, i)

source code 

For each class the sum of the distances to the k nearest neighbors is computed. The distance is computed using the given kernel

Overrides: baseClassifiers.Classifier.classify

test(self, data, **args)

source code 
test a classifier on a given dataset
Parameters:
  • classifier - a trained classifier
  • data - a dataset
  • stats - whether to compute the statistics of the match between the predicted labels and the given labels [True by default]
Returns:
a Results class instance
Overrides: evaluators.assess.test
(inherited documentation)