SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
KNN.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2006 Christian Gehl
8  * Written (W) 1999-2009 Soeren Sonnenburg
9  * Written (W) 2011 Sergey Lisitsyn
10  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
11  */
12 
13 #ifndef _KNN_H__
14 #define _KNN_H__
15 
16 #include <stdio.h>
17 #include <shogun/lib/common.h>
18 #include <shogun/io/SGIO.h>
22 
23 namespace shogun
24 {
25 class CDistanceMachine;
26 
53 class CKNN : public CDistanceMachine
54 {
55  public:
57  CKNN();
58 
65  CKNN(int32_t k, CDistance* d, CLabels* trainlab);
66  virtual ~CKNN();
67 
72  virtual inline EClassifierType get_classifier_type() { return CT_KNN; }
73  //inline EDistanceType get_distance_type() { return DT_KNN;}
74 
79  virtual CLabels* apply();
80 
86  virtual CLabels* apply(CFeatures* data);
87 
89  virtual float64_t apply(int32_t vec_idx)
90  {
91  SG_ERROR( "for performance reasons use apply() instead of apply(int32_t vec_idx)\n");
92  return 0;
93  }
94 
99 
105  virtual bool load(FILE* srcfile);
106 
112  virtual bool save(FILE* dstfile);
113 
118  inline void set_k(int32_t k)
119  {
120  ASSERT(k>0);
121  m_k=k;
122  }
123 
128  inline int32_t get_k()
129  {
130  return m_k;
131  }
132 
136  inline void set_q(float64_t q)
137  {
138  ASSERT(q<=1.0 && q>0.0);
139  m_q = q;
140  }
141 
145  inline float64_t get_q() { return m_q; }
146 
148  inline virtual const char* get_name() const { return "KNN"; }
149 
150  protected:
155  virtual void store_model_features();
156 
160  virtual CLabels* classify_NN();
161 
165  void init_distance(CFeatures* data);
166 
175  virtual bool train_machine(CFeatures* data=NULL);
176 
177  private:
178  void init();
179 
180  protected:
182  int32_t m_k;
183 
186 
188  int32_t num_classes;
189 
191  int32_t min_label;
192 
195 };
196 }
197 #endif
virtual void store_model_features()
Definition: KNN.cpp:319
virtual bool save(FILE *dstfile)
Definition: KNN.cpp:312
Class Distance, a base class for all the distances used in the Shogun toolbox.
Definition: Distance.h:77
EClassifierType
Definition: Machine.h:27
void init_distance(CFeatures *data)
Definition: KNN.cpp:291
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:35
float64_t get_q()
Definition: KNN.h:145
SGMatrix< int32_t > classify_for_multiple_k()
Definition: KNN.cpp:226
#define SG_ERROR(...)
Definition: SGIO.h:75
virtual float64_t apply(int32_t vec_idx)
get output for example &quot;vec_idx&quot;
Definition: KNN.h:89
virtual CLabels * classify_NN()
Definition: KNN.cpp:181
int32_t get_k()
Definition: KNN.h:128
virtual bool train_machine(CFeatures *data=NULL)
Definition: KNN.cpp:62
void set_q(float64_t q)
Definition: KNN.h:136
A generic DistanceMachine interface.
virtual bool load(FILE *srcfile)
Definition: KNN.cpp:305
virtual CLabels * apply()
Definition: KNN.cpp:101
int32_t m_k
the k parameter in KNN
Definition: KNN.h:182
#define ASSERT(x)
Definition: SGIO.h:102
int32_t num_classes
number of classes (i.e. number of values labels can take)
Definition: KNN.h:188
int32_t min_label
smallest label, i.e. -1
Definition: KNN.h:191
double float64_t
Definition: common.h:56
SGVector< int32_t > train_labels
Definition: KNN.h:194
Class KNN, an implementation of the standard k-nearest neigbor classifier.
Definition: KNN.h:53
float64_t m_q
parameter q of rank weighting
Definition: KNN.h:185
shogun matrix
void set_k(int32_t k)
Definition: KNN.h:118
virtual const char * get_name() const
Definition: KNN.h:148
virtual ~CKNN()
Definition: KNN.cpp:57
The class Features is the base class of all feature objects.
Definition: Features.h:56
virtual EClassifierType get_classifier_type()
Definition: KNN.h:72

SHOGUN Machine Learning Toolbox - Documentation