yat
0.14.5pre
|
Nearest Neighbor Classifier. More...
#include <yat/classifier/KNN.h>
Public Member Functions | |
KNN (void) | |
Default constructor. More... | |
KNN (const Distance &) | |
Constructor using an intialized distance measure. More... | |
virtual | ~KNN () |
unsigned int | k () const |
Get the number of nearest neighbors. More... | |
void | k (unsigned int k_in) |
Set the number of nearest neighbors. More... | |
KNN< Distance, NeighborWeighting > * | make_classifier (void) const |
Create an untrained copy of the classifier. More... | |
void | predict (const MatrixLookup &data, utility::Matrix &results) const |
Make predictions for unweighted test data. More... | |
void | predict (const MatrixLookupWeighted &data, utility::Matrix &results) const |
Make predictions for weighted test data. More... | |
void | train (const MatrixLookup &data, const Target &targets) |
Train the KNN using unweighted training data with known targets. More... | |
void | train (const MatrixLookupWeighted &data, const Target &targets) |
Train the KNN using weighted training data with known targets. More... | |
Nearest Neighbor Classifier.
A sample is predicted based on the classes of its k nearest neighbors among the training data samples. KNN supports using different measures, for example, Euclidean distance, to define distance between samples. KNN also supports using different ways to weight the votes of the k nearest neighbors. For example, using a uniform vote a test sample gets a vote for each class which is the number of nearest neighbors belonging to the class.
The template argument Distance should be a class modelling the concept Distance. The template argument NeighborWeighting should be a class modelling the concept Neighbor Weighting Method.
theplu::yat::classifier::KNN< Distance, NeighborWeighting >::KNN | ( | void | ) |
Default constructor.
The number of nearest neighbors (k) is set to 3. Distance and NeighborWeighting are initialized using their default constructuors.
theplu::yat::classifier::KNN< Distance, NeighborWeighting >::KNN | ( | const Distance & | dist | ) |
Constructor using an intialized distance measure.
The number of nearest neighbors (k) is set to
|
virtual |
Destructor
unsigned int theplu::yat::classifier::KNN< Distance, NeighborWeighting >::k | ( | ) | const |
Get the number of nearest neighbors.
void theplu::yat::classifier::KNN< Distance, NeighborWeighting >::k | ( | unsigned int | k_in | ) |
Set the number of nearest neighbors.
Sets the number of neighbors to k_in.
|
virtual |
Create an untrained copy of the classifier.
An interface for making new classifier objects. This function allows for specification at run-time of which classifier type to instatiate (see 'Prototype' in Design Patterns). Derived classes should implement this function with DerivedClass* as the return type and not SupervisedClassifier*. A dynamically allocated DerivedClassifier should be returned. The implementation of this function should correspond to a copy constructor with the exception that the returned classifier is not trained.
Implements theplu::yat::classifier::SupervisedClassifier.
|
virtual |
Make predictions for unweighted test data.
Predictions are calculated and returned in results. For each sample in data, results contains the weighted number of nearest neighbors which belong to each class. Numbers of nearest neighbors are weighted according to NeighborWeighting. If a class has no training samples NaN's are returned for this class in results.
Implements theplu::yat::classifier::SupervisedClassifier.
|
virtual |
Make predictions for weighted test data.
Predictions are calculated and returned in results. For each sample in data, results contains the weighted number of nearest neighbors which belong to each class as in predict(const MatrixLookup& data, utility::Matrix& results). If a test and training sample pair has no variables with non-zero weights in common, there are no variables which can be used to calculate the distance between the two samples. In this case the distance between the two is set to infinity.
Implements theplu::yat::classifier::SupervisedClassifier.
|
virtual |
Train the KNN using unweighted training data with known targets.
For KNN there is no actual training; the entire training data set is stored with targets. KNN only stores references to data and targets as copying these would make the classifier slow. If the number of training samples set is smaller than k, k is set to the number of training samples.
Implements theplu::yat::classifier::SupervisedClassifier.
|
virtual |
Train the KNN using weighted training data with known targets.
See train(const MatrixLookup& data, const Target& targets) for additional information.
Implements theplu::yat::classifier::SupervisedClassifier.