A Binary k-Nearest Neighbour classifier. More...
#include <clsfy_k_nearest_neighbour.h>
Public Member Functions | |
clsfy_k_nearest_neighbour () | |
Construct a KNN classifier. | |
virtual unsigned | classify (const vnl_vector< double > &input) const |
Return the classification of the given probe vector. | |
virtual void | class_probabilities (vcl_vector< double > &outputs, const vnl_vector< double > &input) const |
Provides a probability-like value that the input being in each class. | |
virtual double | log_l (const vnl_vector< double > &input) const |
This value has properties of a Log likelihood of being in class (binary classifiers only). | |
void | set (const vcl_vector< vnl_vector< double > > &inputs, const vcl_vector< unsigned > &outputs) |
Set the training data. | |
unsigned | k () const |
The number of nearest neighbours to look for. | |
void | set_k (unsigned k) |
Set the number of nearest neighbours to look for. | |
const vcl_vector< vnl_vector < double > > & | training_vectors () const |
Return a reference to the training vectors. | |
const vcl_vector< unsigned > & | training_classes () const |
Return a reference to the training classes. | |
virtual unsigned | n_classes () const |
The number of possible output classes. | |
virtual unsigned | n_dims () const |
The dimensionality of input vectors. | |
virtual short | version_no () const |
Storage version number. | |
virtual vcl_string | is_a () const |
Name of the class. | |
virtual bool | is_class (vcl_string const &s) const |
Name of the class. | |
virtual clsfy_classifier_base * | clone () const |
Create a copy on the heap and return base class pointer. | |
virtual void | print_summary (vcl_ostream &os) const |
Print class to os. | |
virtual void | b_write (vsl_b_ostream &bfs) const |
Save class to binary file stream. | |
virtual void | b_read (vsl_b_istream &bfs) |
Load class from binary file stream. | |
virtual void | classify_many (vcl_vector< unsigned > &outputs, mbl_data_wrapper< vnl_vector< double > > &inputs) const |
Classify many input vectors. | |
Private Attributes | |
unsigned | k_ |
The number of nearest neighbours to look for. | |
vcl_vector< vnl_vector< double > > | trainInputs_ |
The set of training input values. Size should equal number of outputs. | |
vcl_vector< unsigned > | trainOutputs_ |
The set of training output values. Size should equal number of inputs. |
A Binary k-Nearest Neighbour classifier.
Definition at line 19 of file clsfy_k_nearest_neighbour.h.
clsfy_k_nearest_neighbour::clsfy_k_nearest_neighbour | ( | ) | [inline] |
Construct a KNN classifier.
Definition at line 32 of file clsfy_k_nearest_neighbour.h.
void clsfy_k_nearest_neighbour::b_read | ( | vsl_b_istream & | bfs | ) | [virtual] |
Load class from binary file stream.
Implements clsfy_classifier_base.
Definition at line 147 of file clsfy_k_nearest_neighbour.cxx.
void clsfy_k_nearest_neighbour::b_write | ( | vsl_b_ostream & | bfs | ) | const [virtual] |
Save class to binary file stream.
Implements clsfy_classifier_base.
Definition at line 137 of file clsfy_k_nearest_neighbour.cxx.
void clsfy_k_nearest_neighbour::class_probabilities | ( | vcl_vector< double > & | outputs, |
const vnl_vector< double > & | input | ||
) | const [virtual] |
Provides a probability-like value that the input being in each class.
Return a probability like value that the input being in each class.
output(i) i<nClasses, contains the probability that the input is in class i
output(i) i<<nClasses, contains the probability that the input is in class i;
Implements clsfy_classifier_base.
Definition at line 57 of file clsfy_k_nearest_neighbour.cxx.
unsigned clsfy_k_nearest_neighbour::classify | ( | const vnl_vector< double > & | input | ) | const [virtual] |
Return the classification of the given probe vector.
Reimplemented from clsfy_classifier_base.
Definition at line 34 of file clsfy_k_nearest_neighbour.cxx.
void clsfy_classifier_base::classify_many | ( | vcl_vector< unsigned > & | outputs, |
mbl_data_wrapper< vnl_vector< double > > & | inputs | ||
) | const [virtual, inherited] |
Classify many input vectors.
Definition at line 55 of file clsfy_classifier_base.cxx.
clsfy_classifier_base * clsfy_k_nearest_neighbour::clone | ( | ) | const [virtual] |
Create a copy on the heap and return base class pointer.
Implements clsfy_classifier_base.
Definition at line 123 of file clsfy_k_nearest_neighbour.cxx.
vcl_string clsfy_k_nearest_neighbour::is_a | ( | ) | const [virtual] |
Name of the class.
Reimplemented from clsfy_classifier_base.
Definition at line 102 of file clsfy_k_nearest_neighbour.cxx.
bool clsfy_k_nearest_neighbour::is_class | ( | vcl_string const & | s | ) | const [virtual] |
Name of the class.
Reimplemented from clsfy_classifier_base.
Definition at line 109 of file clsfy_k_nearest_neighbour.cxx.
unsigned clsfy_k_nearest_neighbour::k | ( | ) | const [inline] |
The number of nearest neighbours to look for.
The default value is 1.
Definition at line 50 of file clsfy_k_nearest_neighbour.h.
double clsfy_k_nearest_neighbour::log_l | ( | const vnl_vector< double > & | input | ) | const [virtual] |
This value has properties of a Log likelihood of being in class (binary classifiers only).
class probability = exp(logL) / (1+exp(logL))
Implements clsfy_classifier_base.
Definition at line 92 of file clsfy_k_nearest_neighbour.cxx.
virtual unsigned clsfy_k_nearest_neighbour::n_classes | ( | ) | const [inline, virtual] |
The number of possible output classes.
Implements clsfy_classifier_base.
Definition at line 65 of file clsfy_k_nearest_neighbour.h.
unsigned clsfy_k_nearest_neighbour::n_dims | ( | ) | const [virtual] |
The dimensionality of input vectors.
Implements clsfy_classifier_base.
Definition at line 79 of file clsfy_k_nearest_neighbour.cxx.
void clsfy_k_nearest_neighbour::print_summary | ( | vcl_ostream & | os | ) | const [virtual] |
Print class to os.
Implements clsfy_classifier_base.
Definition at line 130 of file clsfy_k_nearest_neighbour.cxx.
void clsfy_k_nearest_neighbour::set | ( | const vcl_vector< vnl_vector< double > > & | inputs, |
const vcl_vector< unsigned > & | outputs | ||
) |
Set the training data.
Definition at line 19 of file clsfy_k_nearest_neighbour.cxx.
void clsfy_k_nearest_neighbour::set_k | ( | unsigned | k | ) | [inline] |
Set the number of nearest neighbours to look for.
The default value is 1.
Definition at line 54 of file clsfy_k_nearest_neighbour.h.
const vcl_vector<unsigned >& clsfy_k_nearest_neighbour::training_classes | ( | ) | const [inline] |
Return a reference to the training classes.
Vectors are ordered similarly to training_vectors()
Definition at line 62 of file clsfy_k_nearest_neighbour.h.
const vcl_vector<vnl_vector<double> >& clsfy_k_nearest_neighbour::training_vectors | ( | ) | const [inline] |
Return a reference to the training vectors.
Vectors are ordered similarly to training_classes()
Definition at line 58 of file clsfy_k_nearest_neighbour.h.
short clsfy_k_nearest_neighbour::version_no | ( | ) | const [virtual] |
Storage version number.
Definition at line 116 of file clsfy_k_nearest_neighbour.cxx.
unsigned clsfy_k_nearest_neighbour::k_ [private] |
The number of nearest neighbours to look for.
Definition at line 22 of file clsfy_k_nearest_neighbour.h.
vcl_vector<vnl_vector<double> > clsfy_k_nearest_neighbour::trainInputs_ [private] |
The set of training input values. Size should equal number of outputs.
Definition at line 25 of file clsfy_k_nearest_neighbour.h.
vcl_vector<unsigned> clsfy_k_nearest_neighbour::trainOutputs_ [private] |
The set of training output values. Size should equal number of inputs.
Definition at line 28 of file clsfy_k_nearest_neighbour.h.