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. Formally, kNN is described as
This class provides a capability to do weighted classfication using:
where
.
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. And finally, in case of k=1 classification will take less time with an special optimization provided.
Definition at line 53 of file KNN.h.
Public Member Functions |
| | CKNN () |
| | CKNN (int32_t k, CDistance *d, CLabels *trainlab) |
| virtual | ~CKNN () |
| virtual EClassifierType | get_classifier_type () |
| virtual CLabels * | apply () |
| virtual CLabels * | apply (CFeatures *data) |
| virtual float64_t | apply (int32_t vec_idx) |
| | get output for example "vec_idx"
|
| SGMatrix< int32_t > | classify_for_multiple_k () |
| virtual bool | load (FILE *srcfile) |
| virtual bool | save (FILE *dstfile) |
| void | set_k (int32_t k) |
| int32_t | get_k () |
| void | set_q (float64_t q) |
| float64_t | get_q () |
| virtual const char * | get_name () const |
| | CDistanceMachine () |
| virtual | ~CDistanceMachine () |
| void | set_distance (CDistance *d) |
| CDistance * | get_distance () |
| void | distances_lhs (float64_t *result, int32_t idx_a1, int32_t idx_a2, int32_t idx_b) |
| void | distances_rhs (float64_t *result, int32_t idx_b1, int32_t idx_b2, int32_t idx_a) |
| | CMachine () |
| virtual | ~CMachine () |
| virtual bool | train (CFeatures *data=NULL) |
| virtual void | set_labels (CLabels *lab) |
| virtual CLabels * | get_labels () |
| virtual float64_t | get_label (int32_t i) |
| void | set_max_train_time (float64_t t) |
| float64_t | get_max_train_time () |
| void | set_solver_type (ESolverType st) |
| ESolverType | get_solver_type () |
| virtual void | set_store_model_features (bool store_model) |
| | CSGObject () |
| | CSGObject (const CSGObject &orig) |
| virtual | ~CSGObject () |
| virtual bool | is_generic (EPrimitiveType *generic) const |
| template<class T > |
| void | set_generic () |
| void | unset_generic () |
| virtual void | print_serializable (const char *prefix="") |
| virtual bool | save_serializable (CSerializableFile *file, const char *prefix="") |
| virtual bool | load_serializable (CSerializableFile *file, const char *prefix="") |
| void | set_global_io (SGIO *io) |
| SGIO * | get_global_io () |
| void | set_global_parallel (Parallel *parallel) |
| Parallel * | get_global_parallel () |
| void | set_global_version (Version *version) |
| Version * | get_global_version () |
| SGVector< char * > | get_modelsel_names () |
| char * | get_modsel_param_descr (const char *param_name) |
| index_t | get_modsel_param_index (const char *param_name) |