Public Member Functions | Protected Attributes

CKNN Class Reference

Detailed Description

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.

Inheritance diagram for CKNN:
Inheritance graph

List of all members.

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 CLabelsclassify ()
virtual CLabelsclassify (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

Constructor & Destructor Documentation

CKNN (  ) 

default constructor

Definition at line 20 of file KNN.cpp.

CKNN ( int32_t  k,
CDistance d,
CLabels trainlab 


k k
d distance
trainlab labels for training

Definition at line 25 of file KNN.cpp.

~CKNN (  )  [virtual]

Definition at line 37 of file KNN.cpp.

Member Function Documentation

CLabels * classify (  )  [virtual]

classify all examples

resulting labels

histogram of classes and returned output

Implements CDistanceMachine.

Definition at line 78 of file KNN.cpp.

CLabels * classify ( CFeatures data  )  [virtual]

classify objects

data (test)data to be classified
classified labels

Implements CDistanceMachine.

Definition at line 147 of file KNN.cpp.

virtual float64_t classify_example ( int32_t  vec_idx  )  [virtual]

get output for example "vec_idx"

Reimplemented from CClassifier.

Definition at line 85 of file KNN.h.

void classify_for_multiple_k ( int32_t **  output,
int32_t *  num_vec,
int32_t *  k_out 

classify all examples for 1...k

output resulting labels for all k
k_out number of columns (k)
num_vec number of outputs

histogram of classes and returned output

Definition at line 164 of file KNN.cpp.

virtual EClassifierType get_classifier_type (  )  [virtual]

get classifier type

classifier type KNN

Reimplemented from CClassifier.

Definition at line 58 of file KNN.h.

int32_t get_k (  ) 

get k


Definition at line 127 of file KNN.h.

virtual const char* get_name ( void   )  const [virtual]
object name

Reimplemented from CDistanceMachine.

Definition at line 133 of file KNN.h.

bool load ( FILE *  srcfile  )  [virtual]

load from file

srcfile file to load from
if loading was successful

Reimplemented from CClassifier.

Definition at line 235 of file KNN.cpp.

bool save ( FILE *  dstfile  )  [virtual]

save to file

dstfile file to save to
if saving was successful

Reimplemented from CClassifier.

Definition at line 242 of file KNN.cpp.

void set_k ( int32_t  p_k  ) 

set k

p_k new k

Definition at line 117 of file KNN.h.

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)
whether training was successful

Reimplemented from CClassifier.

Definition at line 42 of file KNN.cpp.

Member Data Documentation

int32_t k [protected]

the k parameter in KNN

Definition at line 137 of file KNN.h.

int32_t min_label [protected]

smallest label, i.e. -1

Definition at line 143 of file KNN.h.

int32_t num_classes [protected]

number of classes (i.e. number of values labels can take)

Definition at line 140 of file KNN.h.

int32_t num_train_labels [protected]

number of train examples

Definition at line 146 of file KNN.h.

int32_t* train_labels [protected]

the actual trainlabels

Definition at line 149 of file KNN.h.

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation