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

SHOGUN Machine Learning Toolbox - Documentation