Public Member Functions | Protected 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. Formally, kNN is described as

\[ label for x = \arg \max_{l} \sum_{i=1}^{k} [label of i-th example = l] \]

This class provides a capability to do weighted classfication using:

\[ label for x = \arg \max_{l} \sum_{i=1}^{k} [label of i-th example = l] q^{i}, \]

where $|q|<1$.

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.

Inheritance diagram for CKNN:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 CKNN ()
 CKNN (int32_t k, CDistance *d, CLabels *trainlab)
virtual ~CKNN ()
virtual EClassifierType get_classifier_type ()
virtual CLabelsapply ()
virtual CLabelsapply (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

Protected Member Functions

virtual void store_model_features ()
virtual CLabelsclassify_NN ()
void init_distance (CFeatures *data)
virtual bool train_machine (CFeatures *data=NULL)

Protected Attributes

int32_t m_k
 the k parameter in KNN
float64_t m_q
 parameter q of rank weighting
int32_t num_classes
 number of classes (i.e. number of values labels can take)
int32_t min_label
 smallest label, i.e. -1
SGVector< int32_t > train_labels

Constructor & Destructor Documentation

CKNN (  ) 

default constructor

Definition at line 21 of file KNN.cpp.

CKNN ( int32_t  k,
CDistance d,
CLabels trainlab 
)

constructor

Parameters:
k k
d distance
trainlab labels for training

Definition at line 27 of file KNN.cpp.

~CKNN (  )  [virtual]

Definition at line 57 of file KNN.cpp.


Member Function Documentation

CLabels * apply (  )  [virtual]

classify all examples

Returns:
resulting labels

histogram of classes and returned output

Reimplemented from CDistanceMachine.

Definition at line 101 of file KNN.cpp.

virtual float64_t apply ( int32_t  vec_idx  )  [virtual]

get output for example "vec_idx"

Reimplemented from CDistanceMachine.

Definition at line 89 of file KNN.h.

CLabels * apply ( CFeatures data  )  [virtual]

classify objects

Parameters:
data (test)data to be classified
Returns:
classified labels

Reimplemented from CDistanceMachine.

Definition at line 170 of file KNN.cpp.

SGMatrix< int32_t > classify_for_multiple_k (  ) 

classify all examples for 1...k

histogram of classes and returned output

Definition at line 226 of file KNN.cpp.

CLabels * classify_NN (  )  [protected, virtual]

classify all examples with nearest neighbor (k=1)

Returns:
classified labels

Definition at line 181 of file KNN.cpp.

virtual EClassifierType get_classifier_type (  )  [virtual]

get classifier type

Returns:
classifier type KNN

Reimplemented from CMachine.

Definition at line 72 of file KNN.h.

int32_t get_k (  ) 

get k

Returns:
value of k

Definition at line 128 of file KNN.h.

virtual const char* get_name (  )  const [virtual]
Returns:
object name

Reimplemented from CDistanceMachine.

Definition at line 148 of file KNN.h.

float64_t get_q (  ) 

get q

Returns:
q parameter

Definition at line 145 of file KNN.h.

void init_distance ( CFeatures data  )  [protected]

init distances to test examples

Parameters:
data test examples

Definition at line 291 of file KNN.cpp.

bool load ( FILE *  srcfile  )  [virtual]

load from file

Parameters:
srcfile file to load from
Returns:
if loading was successful

Reimplemented from CMachine.

Definition at line 305 of file KNN.cpp.

bool save ( FILE *  dstfile  )  [virtual]

save to file

Parameters:
dstfile file to save to
Returns:
if saving was successful

Reimplemented from CMachine.

Definition at line 312 of file KNN.cpp.

void set_k ( int32_t  k  ) 

set k

Parameters:
k k to be set

Definition at line 118 of file KNN.h.

void set_q ( float64_t  q  ) 

set q

Parameters:
q value

Definition at line 136 of file KNN.h.

void store_model_features (  )  [protected, virtual]

Stores feature data of underlying model.

Replaces lhs and rhs of underlying distance with copies of themselves

Reimplemented from CDistanceMachine.

Definition at line 319 of file KNN.cpp.

bool train_machine ( CFeatures data = NULL  )  [protected, virtual]

train k-NN classifier

Parameters:
data training data (parameter can be avoided if distance or kernel-based classifiers are used and distance/kernels are initialized with train data)
Returns:
whether training was successful

Reimplemented from CMachine.

Definition at line 62 of file KNN.cpp.


Member Data Documentation

int32_t m_k [protected]

the k parameter in KNN

Definition at line 182 of file KNN.h.

float64_t m_q [protected]

parameter q of rank weighting

Definition at line 185 of file KNN.h.

int32_t min_label [protected]

smallest label, i.e. -1

Definition at line 191 of file KNN.h.

int32_t num_classes [protected]

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

Definition at line 188 of file KNN.h.

SGVector<int32_t> train_labels [protected]

the actual trainlabels

Definition at line 194 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