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 39 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 (CFeatures *data=NULL) |
| virtual CLabels * | classify () |
| virtual CLabels * | classify (CFeatures *data) |
| virtual float64_t | classify_example (int32_t vec_idx) |
| get output for example "vec_idx" | |
| void | classify_for_multiple_k (int32_t **output, int32_t *num_vec, int32_t *k_out) |
| virtual bool | load (FILE *srcfile) |
| virtual bool | save (FILE *dstfile) |
| void | set_k (int32_t p_k) |
| int32_t | get_k () |
| virtual const char * | get_name () const |
Protected Attributes | |
| int32_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 | |
| CLabels * classify | ( | ) | [virtual] |
classify all examples
histogram of classes and returned output
Implements CDistanceMachine.
classify objects
| data | (test)data to be classified |
Implements CDistanceMachine.
| virtual float64_t classify_example | ( | int32_t | vec_idx | ) | [virtual] |
get output for example "vec_idx"
Reimplemented from CClassifier.
| void classify_for_multiple_k | ( | int32_t ** | output, | |
| int32_t * | num_vec, | |||
| int32_t * | k_out | |||
| ) |
| virtual EClassifierType get_classifier_type | ( | ) | [virtual] |
| virtual const char* get_name | ( | void | ) | const [virtual] |
| bool load | ( | FILE * | srcfile | ) | [virtual] |
load from file
| srcfile | file to load from |
Reimplemented from CClassifier.
| bool save | ( | FILE * | dstfile | ) | [virtual] |
save to file
| dstfile | file to save to |
Reimplemented from CClassifier.
| bool train | ( | CFeatures * | data = NULL |
) | [virtual] |
train k-NN classifier
| data | training data (parameter can be avoided if distance or kernel-based classifiers are used and distance/kernels are initialized with train data) |
Reimplemented from CClassifier.
int32_t num_classes [protected] |
int32_t num_train_labels [protected] |
int32_t* train_labels [protected] |