SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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  * Written (W) 2012 Fernando José Iglesias García, cover tree support
11  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
12  */
13 
14 #ifndef _KNN_H__
15 #define _KNN_H__
16 
17 #include <shogun/lib/config.h>
18 
19 #include <shogun/lib/common.h>
20 #include <shogun/io/SGIO.h>
24 
25 namespace shogun
26 {
28  {
32 #ifdef HAVE_CXX11
33  KNN_LSH
34 #endif
35  };
36 
37 class CDistanceMachine;
38 
68 class CKNN : public CDistanceMachine
69 {
70  public:
72 
73 
74  CKNN();
75 
82  CKNN(int32_t k, CDistance* d, CLabels* trainlab, KNN_SOLVER knn_solver=KNN_BRUTE);
83 
84  virtual ~CKNN();
85 
90  virtual EMachineType get_classifier_type() { return CT_KNN; }
91 
102 
108  virtual CMulticlassLabels* apply_multiclass(CFeatures* data=NULL);
109 
111  virtual float64_t apply_one(int32_t vec_idx)
112  {
113  SG_ERROR("for performance reasons use apply() instead of apply(int32_t vec_idx)\n")
114  return 0;
115  }
116 
121 
127  virtual bool load(FILE* srcfile);
128 
134  virtual bool save(FILE* dstfile);
135 
140  inline void set_k(int32_t k)
141  {
142  ASSERT(k>0)
143  m_k=k;
144  }
145 
150  inline int32_t get_k()
151  {
152  return m_k;
153  }
154 
158  inline void set_q(float64_t q)
159  {
160  ASSERT(q<=1.0 && q>0.0)
161  m_q = q;
162  }
163 
167  inline float64_t get_q() { return m_q; }
168 
172  inline int32_t get_leaf_size() const {return m_leaf_size; }
173 
177  inline void set_leaf_size(int32_t leaf_size)
178  {
179  m_leaf_size = leaf_size;
180  }
181 
183  virtual const char* get_name() const { return "KNN"; }
184 
189  {
190  return m_knn_solver;
191  }
192 
197  inline void set_knn_solver_type(KNN_SOLVER knn_solver)
198  {
199  m_knn_solver = knn_solver;
200  }
201 
202 #ifdef HAVE_CXX11
203 
207  inline void set_lsh_parameters(int32_t l, int32_t t)
208  {
209  m_lsh_l = l;
210  m_lsh_t = t;
211  }
212 #endif
213 
214  protected:
219  virtual void store_model_features();
220 
224  virtual CMulticlassLabels* classify_NN();
225 
229  void init_distance(CFeatures* data);
230 
239  virtual bool train_machine(CFeatures* data=NULL);
240 
241  private:
242  void init();
243 
256  int32_t choose_class(float64_t* classes, int32_t* train_lab);
257 
270  void choose_class_for_multiple_k(int32_t* output, int32_t* classes, int32_t* train_lab, int32_t step);
271 
272  protected:
274  int32_t m_k;
275 
278 
280  int32_t m_num_classes;
281 
283  int32_t m_min_label;
284 
287 
289 
290  int32_t m_leaf_size;
291 
292 #ifdef HAVE_CXX11
293  /* Number of hash tables for LSH */
294  int32_t m_lsh_l;
295 
296  /* Number of probes per query for LSH */
297  int32_t m_lsh_t;
298 #endif
299 };
300 
301 }
302 #endif
EMachineType
Definition: Machine.h:33
virtual void store_model_features()
Definition: KNN.cpp:565
virtual bool save(FILE *dstfile)
Definition: KNN.cpp:558
virtual EMachineType get_classifier_type()
Definition: KNN.h:90
Class Distance, a base class for all the distances used in the Shogun toolbox.
Definition: Distance.h:87
void init_distance(CFeatures *data)
Definition: KNN.cpp:537
KNN_SOLVER m_knn_solver
Definition: KNN.h:288
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
float64_t get_q()
Definition: KNN.h:167
SGMatrix< int32_t > classify_for_multiple_k()
Definition: KNN.cpp:415
KNN_SOLVER
Definition: KNN.h:27
#define SG_ERROR(...)
Definition: SGIO.h:129
int32_t get_k()
Definition: KNN.h:150
int32_t m_min_label
smallest label, i.e. -1
Definition: KNN.h:283
virtual bool train_machine(CFeatures *data=NULL)
Definition: KNN.cpp:85
void set_q(float64_t q)
Definition: KNN.h:158
SGMatrix< index_t > nearest_neighbors()
Definition: KNN.cpp:122
A generic DistanceMachine interface.
virtual bool load(FILE *srcfile)
Definition: KNN.cpp:551
int32_t m_num_classes
number of classes (i.e. number of values labels can take)
Definition: KNN.h:280
Multiclass Labels for multi-class classification.
void set_leaf_size(int32_t leaf_size)
Definition: KNN.h:177
int32_t m_k
the k parameter in KNN
Definition: KNN.h:274
#define ASSERT(x)
Definition: SGIO.h:201
#define MACHINE_PROBLEM_TYPE(PT)
Definition: Machine.h:120
double float64_t
Definition: common.h:50
int32_t get_leaf_size() const
Definition: KNN.h:172
Class KNN, an implementation of the standard k-nearest neigbor classifier.
Definition: KNN.h:68
KNN_SOLVER get_knn_solver_type()
Definition: KNN.h:188
float64_t m_q
parameter q of rank weighting
Definition: KNN.h:277
SGVector< int32_t > m_train_labels
Definition: KNN.h:286
void set_k(int32_t k)
Definition: KNN.h:140
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual const char * get_name() const
Definition: KNN.h:183
virtual ~CKNN()
Definition: KNN.cpp:81
void set_knn_solver_type(KNN_SOLVER knn_solver)
Definition: KNN.h:197
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual CMulticlassLabels * classify_NN()
Definition: KNN.cpp:366
virtual CMulticlassLabels * apply_multiclass(CFeatures *data=NULL)
Definition: KNN.cpp:170
virtual float64_t apply_one(int32_t vec_idx)
get output for example "vec_idx"
Definition: KNN.h:111
int32_t m_leaf_size
Definition: KNN.h:290

SHOGUN Machine Learning Toolbox - Documentation