SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
List of all members | 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]

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
- Public Member Functions inherited from CDistanceMachine
 CDistanceMachine ()
virtual ~CDistanceMachine ()
void set_distance (CDistance *d)
CDistanceget_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)
- Public Member Functions inherited from CMachine
 CMachine ()
virtual ~CMachine ()
virtual bool train (CFeatures *data=NULL)
virtual void set_labels (CLabels *lab)
virtual CLabelsget_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)
- Public Member Functions inherited from CSGObject
 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)
SGIOget_global_io ()
void set_global_parallel (Parallel *parallel)
Parallelget_global_parallel ()
void set_global_version (Version *version)
Versionget_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)

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
- Protected Attributes inherited from CDistanceMachine
CDistancedistance
- Protected Attributes inherited from CMachine
float64_t max_train_time
CLabelslabels
ESolverType solver_type
bool m_store_model_features

Additional Inherited Members

- Public Attributes inherited from CSGObject
SGIOio
Parallelparallel
Versionversion
Parameterm_parameters
Parameterm_model_selection_parameters
- Static Protected Member Functions inherited from CDistanceMachine
static void * run_distance_thread_lhs (void *p)
static void * run_distance_thread_rhs (void *p)

Constructor & Destructor Documentation

CKNN ( )

default constructor

Definition at line 21 of file KNN.cpp.

CKNN ( int32_t  k,
CDistance d,
CLabels trainlab 
)

constructor

Parameters
kk
ddistance
trainlablabels 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.

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.

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.

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 ( )
protectedvirtual

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
datatest examples

Definition at line 291 of file KNN.cpp.

bool load ( FILE *  srcfile)
virtual

load from file

Parameters
srcfilefile 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
dstfilefile 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
kk to be set

Definition at line 118 of file KNN.h.

void set_q ( float64_t  q)

set q

Parameters
qvalue

Definition at line 136 of file KNN.h.

void store_model_features ( )
protectedvirtual

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)
protectedvirtual

train k-NN classifier

Parameters
datatraining 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:

SHOGUN Machine Learning Toolbox - Documentation