yat  0.14.5pre
Public Member Functions | List of all members
theplu::yat::classifier::KNN< Distance, NeighborWeighting > Class Template Reference

Nearest Neighbor Classifier. More...

#include <yat/classifier/KNN.h>

Inheritance diagram for theplu::yat::classifier::KNN< Distance, NeighborWeighting >:
theplu::yat::classifier::SupervisedClassifier

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...
 

Detailed Description

template<typename Distance, typename NeighborWeighting = KNN_Uniform>
class theplu::yat::classifier::KNN< Distance, NeighborWeighting >

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.

Constructor & Destructor Documentation

template<typename Distance , typename NeighborWeighting >
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.

template<typename Distance , typename NeighborWeighting >
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

  1. NeighborWeighting is initialized using its default constructor. This constructor should be used if Distance has parameters and the user wants to specify the parameters by initializing Distance prior to constructing the KNN.
template<typename Distance , typename NeighborWeighting >
theplu::yat::classifier::KNN< Distance, NeighborWeighting >::~KNN ( )
virtual

Destructor

Member Function Documentation

template<typename Distance , typename NeighborWeighting >
unsigned int theplu::yat::classifier::KNN< Distance, NeighborWeighting >::k ( ) const

Get the number of nearest neighbors.

Returns
The number of neighbors.
template<typename Distance , typename NeighborWeighting >
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.

template<typename Distance , typename NeighborWeighting >
KNN< Distance, NeighborWeighting > * theplu::yat::classifier::KNN< Distance, NeighborWeighting >::make_classifier ( void  ) const
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.

Returns
A dynamically allocated classifier, which has to be deleted by the caller to avoid memory leaks.

Implements theplu::yat::classifier::SupervisedClassifier.

template<typename Distance , typename NeighborWeighting >
void theplu::yat::classifier::KNN< Distance, NeighborWeighting >::predict ( const MatrixLookup data,
utility::Matrix results 
) const
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.

template<typename Distance , typename NeighborWeighting >
void theplu::yat::classifier::KNN< Distance, NeighborWeighting >::predict ( const MatrixLookupWeighted data,
utility::Matrix results 
) const
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.

template<typename Distance , typename NeighborWeighting >
void theplu::yat::classifier::KNN< Distance, NeighborWeighting >::train ( const MatrixLookup data,
const Target targets 
)
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.

Note
If data or targets go out of scope ore are deleted, the KNN becomes invalid and further use is undefined unless it is trained again.

Implements theplu::yat::classifier::SupervisedClassifier.

template<typename Distance , typename NeighborWeighting >
void theplu::yat::classifier::KNN< Distance, NeighborWeighting >::train ( const MatrixLookupWeighted data,
const Target targets 
)
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.


The documentation for this class was generated from the following file:

Generated on Tue Sep 26 2017 02:33:29 for yat by  doxygen 1.8.5