Class KNN, an implementation of the standard k-nearest neigbor classifier.
An example is classified to belong to the class of which the majority of the k closest examples belong to.
To avoid ties, k should be an odd number. To define how close examples are k-NN requires a CDistance object to work with (e.g., CEuclideanDistance ).
Note that k-NN has zero training time but classification times increase dramatically with the number of examples. Also note that k-NN is capable of multi-class-classification.
Definition at line 37 of file KNN.h.

Public Member Functions | |
| CKNN () | |
| CKNN (int32_t k, CDistance *d, CLabels *trainlab) | |
| virtual | ~CKNN () |
| virtual EClassifierType | get_classifier_type () |
| virtual bool | train () |
| virtual CLabels * | classify (CLabels *output=NULL) |
| virtual float64_t | classify_example (int32_t vec_idx) |
| get output for example "vec_idx" | |
| virtual bool | load (FILE *srcfile) |
| virtual bool | save (FILE *dstfile) |
| void | set_k (float64_t p_k) |
| float64_t | get_k () |
| virtual const char * | get_name () const |
Protected Attributes | |
| float64_t | k |
| the k parameter in KNN | |
| int32_t | num_classes |
| number of classes (i.e. number of values labels can take) | |
| int32_t | min_label |
| smallest label, i.e. -1 | |
| int32_t | num_train_labels |
| number of train examples | |
| int32_t * | train_labels |
| the actual trainlabels | |
classify all examples
| output | resulting labels |
histogram of classes and returned output
Reimplemented from CClassifier.
| virtual float64_t CKNN::classify_example | ( | int32_t | vec_idx | ) | [virtual] |
get output for example "vec_idx"
Reimplemented from CClassifier.
| virtual EClassifierType CKNN::get_classifier_type | ( | ) | [virtual] |
| virtual const char* CKNN::get_name | ( | ) | const [virtual] |
| bool CKNN::load | ( | FILE * | srcfile | ) | [virtual] |
load from file
| srcfile | file to load from |
Reimplemented from CClassifier.
| bool CKNN::save | ( | FILE * | dstfile | ) | [virtual] |
save to file
| dstfile | file to save to |
Reimplemented from CClassifier.
| bool CKNN::train | ( | ) | [virtual] |
int32_t CKNN::min_label [protected] |
int32_t CKNN::num_classes [protected] |
int32_t CKNN::num_train_labels [protected] |
int32_t* CKNN::train_labels [protected] |