GestureRecognitionToolkit  Version: 0.2.0
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
KNN.h
Go to the documentation of this file.
1 
42 #ifndef GRT_KNN_HEADER
43 #define GRT_KNN_HEADER
44 
45 #include "../../CoreModules/Classifier.h"
46 
47 GRT_BEGIN_NAMESPACE
48 
49 #define BIG_DISTANCE 99e+99
50 
51 class GRT_API KNN : public Classifier
52 {
53 public:
65  KNN(UINT K=10,bool useScaling=false,bool useNullRejection=false,Float nullRejectionCoeff=10.0,bool searchForBestKValue = false,UINT minKSearchValue = 1,UINT maxKSearchValue = 10);
66 
72  KNN(const KNN &rhs);
73 
77  virtual ~KNN(void);
78 
85  KNN &operator=(const KNN &rhs);
86 
94  virtual bool deepCopyFrom(const Classifier *classifier);
95 
103  virtual bool train_(ClassificationData &trainingData);
104 
112  virtual bool predict_(VectorFloat &inputVector);
113 
120  virtual bool clear();
121 
129  virtual bool save( std::fstream &file ) const;
130 
138  virtual bool load( std::fstream &file );
139 
147  virtual bool recomputeNullRejectionThresholds();
148 
154  UINT getK(){ return K; }
155 
162  UINT getDistanceMethod(){ return distanceMethod; }
163 
164  //Setters
171  bool setK(UINT K);
172 
179  bool setMinKSearchValue(UINT minKSearchValue);
180 
187  bool setMaxKSearchValue(UINT maxKSearchValue);
188 
196  bool enableBestKValueSearch(bool searchForBestKValue);
197 
205  bool setNullRejectionCoeff(Float nullRejectionCoeff);
206 
214  bool setDistanceMethod(UINT distanceMethod);
215 
216  //Tell the compiler we are using the following functions from the MLBase class to stop hidden virtual function warnings
217  using MLBase::save;
218  using MLBase::load;
219  using MLBase::train_;
220  using MLBase::predict_;
221  using MLBase::predict;
222 
223 protected:
224  bool train_(const ClassificationData &trainingData,const UINT K);
225  bool predict(const VectorFloat &inputVector,const UINT K);
226  bool loadLegacyModelFromFile( std::fstream &file );
227  Float computeEuclideanDistance(const VectorFloat &a,const VectorFloat &b);
228  Float computeCosineDistance(const VectorFloat &a,const VectorFloat &b);
229  Float computeManhattanDistance(const VectorFloat &a,const VectorFloat &b);
230 
231  UINT K;
239 
241 
242  public:
243  enum DistanceMethods{EUCLIDEAN_DISTANCE=0,COSINE_DISTANCE,MANHATTAN_DISTANCE};
244 
245 };
246 
247 GRT_END_NAMESPACE
248 
249 #endif //GRT_KNN_HEADER
250 
VectorFloat trainingSigma
Holds the average max-class distance of the training data for each of classes
Definition: KNN.h:238
virtual bool predict(VectorFloat inputVector)
Definition: MLBase.cpp:113
virtual bool recomputeNullRejectionThresholds()
Definition: Classifier.h:237
virtual bool predict_(VectorFloat &inputVector)
Definition: MLBase.cpp:115
UINT getK()
Definition: KNN.h:154
bool searchForBestKValue
The distance method used to compute the distance between each data point
Definition: KNN.h:233
UINT distanceMethod
The number of neighbours to search for
Definition: KNN.h:232
ClassificationData trainingData
The maximum K value to end the search at
Definition: KNN.h:236
UINT maxKSearchValue
The minimum K value to start the search from
Definition: KNN.h:235
virtual bool save(const std::string filename) const
Definition: MLBase.cpp:143
virtual bool load(const std::string filename)
Definition: MLBase.cpp:167
virtual bool deepCopyFrom(const Classifier *classifier)
Definition: Classifier.h:63
UINT getDistanceMethod()
Definition: KNN.h:162
UINT minKSearchValue
Sets if the best K value should be searched for or if the model should be trained with K ...
Definition: KNN.h:234
static RegisterClassifierModule< KNN > registerModule
Holds the stddev of the max-class distance of the training data for each of classes ...
Definition: KNN.h:240
VectorFloat trainingMu
Holds the trainingData to perform the predictions
Definition: KNN.h:237
virtual bool setNullRejectionCoeff(Float nullRejectionCoeff)
Definition: Classifier.cpp:236
virtual bool train_(ClassificationData &trainingData)
Definition: MLBase.cpp:91
virtual bool clear()
Definition: Classifier.cpp:142
Definition: KNN.h:51