GestureRecognitionToolkit  Version: 0.2.5
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 
26 #ifndef GRT_KNN_HEADER
27 #define GRT_KNN_HEADER
28 
29 #include "../../CoreModules/Classifier.h"
30 
31 GRT_BEGIN_NAMESPACE
32 
33 #define BIG_DISTANCE 99e+99
34 
51 class GRT_API KNN : public Classifier
52 {
53 public:
54  enum DistanceMethods{EUCLIDEAN_DISTANCE=0,COSINE_DISTANCE,MANHATTAN_DISTANCE};
55 
67  KNN(UINT K=10,bool useScaling=false,bool useNullRejection=false,Float nullRejectionCoeff=10.0,bool searchForBestKValue = false,UINT minKSearchValue = 1,UINT maxKSearchValue = 10);
68 
74  KNN(const KNN &rhs);
75 
79  virtual ~KNN(void);
80 
87  KNN &operator=(const KNN &rhs);
88 
96  virtual bool deepCopyFrom(const Classifier *classifier);
97 
105  virtual bool train_(ClassificationData &trainingData);
106 
114  virtual bool predict_(VectorFloat &inputVector);
115 
122  virtual bool clear();
123 
131  virtual bool save( std::fstream &file ) const;
132 
140  virtual bool load( std::fstream &file );
141 
149  virtual bool recomputeNullRejectionThresholds();
150 
156  UINT getK(){ return K; }
157 
164  UINT getDistanceMethod(){ return distanceMethod; }
165 
166  //Setters
173  bool setK(UINT K);
174 
181  bool setMinKSearchValue(UINT minKSearchValue);
182 
189  bool setMaxKSearchValue(UINT maxKSearchValue);
190 
198  bool enableBestKValueSearch(bool searchForBestKValue);
199 
207  bool setNullRejectionCoeff(Float nullRejectionCoeff);
208 
216  bool setDistanceMethod(UINT distanceMethod);
217 
223  static std::string getId();
224 
225  //Tell the compiler we are using the following functions from the MLBase class to stop hidden virtual function warnings
226  using MLBase::save;
227  using MLBase::load;
228  using MLBase::train_;
229  using MLBase::predict_;
230  using MLBase::predict;
231 
232 protected:
233  bool train_(const ClassificationData &trainingData,const UINT K);
234  bool predict(const VectorFloat &inputVector,const UINT K);
235  bool loadLegacyModelFromFile( std::fstream &file );
236  Float computeEuclideanDistance(const VectorFloat &a,const VectorFloat &b);
237  Float computeCosineDistance(const VectorFloat &a,const VectorFloat &b);
238  Float computeManhattanDistance(const VectorFloat &a,const VectorFloat &b);
239 
240  UINT K;
248 
249 private:
250  static RegisterClassifierModule< KNN > registerModule;
251  static const std::string id;
252 };
253 
254 GRT_END_NAMESPACE
255 
256 #endif //GRT_KNN_HEADER
257 
VectorFloat trainingSigma
Holds the average max-class distance of the training data for each of classes
Definition: KNN.h:247
std::string getId() const
Definition: GRTBase.cpp:85
virtual bool predict(VectorFloat inputVector)
Definition: MLBase.cpp:135
virtual bool recomputeNullRejectionThresholds()
Definition: Classifier.h:255
virtual bool predict_(VectorFloat &inputVector)
Definition: MLBase.cpp:137
UINT getK()
Definition: KNN.h:156
bool searchForBestKValue
The distance method used to compute the distance between each data point
Definition: KNN.h:242
virtual bool setNullRejectionCoeff(const Float nullRejectionCoeff)
Definition: Classifier.cpp:254
UINT distanceMethod
The number of neighbours to search for
Definition: KNN.h:241
ClassificationData trainingData
The maximum K value to end the search at
Definition: KNN.h:245
virtual bool save(const std::string &filename) const
Definition: MLBase.cpp:167
UINT maxKSearchValue
The minimum K value to start the search from
Definition: KNN.h:244
virtual bool deepCopyFrom(const Classifier *classifier)
Definition: Classifier.h:64
UINT getDistanceMethod()
Definition: KNN.h:164
UINT minKSearchValue
Sets if the best K value should be searched for or if the model should be trained with K ...
Definition: KNN.h:243
VectorFloat trainingMu
Holds the trainingData to perform the predictions
Definition: KNN.h:246
virtual bool train_(ClassificationData &trainingData)
Definition: MLBase.cpp:109
virtual bool load(const std::string &filename)
Definition: MLBase.cpp:190
virtual bool clear()
Definition: Classifier.cpp:151
Definition: KNN.h:51
This is the main base class that all GRT Classification algorithms should inherit from...
Definition: Classifier.h:41