GestureRecognitionToolkit  Version: 0.1.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 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 saveModelToFile( std::fstream &file ) const;
130 
138  virtual bool loadModelFromFile( std::fstream &file );
139 
147  virtual bool recomputeNullRejectionThresholds();
148 
154  UINT getK(){ return K; }
155 
163 
164  //Setters
171  bool setK(UINT K);
172 
180 
188 
197 
205  bool setNullRejectionCoeff(Float nullRejectionCoeff);
206 
215 
216  //Tell the compiler we are using the following functions from the MLBase class to stop hidden virtual function warnings
219  using MLBase::train;
220  using MLBase::train_;
221  using MLBase::predict;
222  using MLBase::predict_;
223 
224 protected:
225  bool train_(const ClassificationData &trainingData,const UINT K);
226  bool predict(const VectorFloat &inputVector,const UINT K);
227  bool loadLegacyModelFromFile( std::fstream &file );
228  Float computeEuclideanDistance(const VectorFloat &a,const VectorFloat &b);
229  Float computeCosineDistance(const VectorFloat &a,const VectorFloat &b);
230  Float computeManhattanDistance(const VectorFloat &a,const VectorFloat &b);
231 
232  UINT K;
240 
242 
243 public:
244  enum DistanceMethods{EUCLIDEAN_DISTANCE=0,COSINE_DISTANCE,MANHATTAN_DISTANCE};
245 
246 };
247 
248 GRT_END_NAMESPACE
249 
250 #endif //GRT_KNN_HEADER
251 
VectorFloat trainingSigma
Holds the average max-class distance of the training data for each of classes
Definition: KNN.h:239
virtual bool predict(VectorFloat inputVector)
Definition: MLBase.cpp:112
virtual bool predict_(VectorFloat &inputVector)
Definition: MLBase.cpp:114
UINT getK()
Definition: KNN.h:154
bool searchForBestKValue
The distance method used to compute the distance between each data point
Definition: KNN.h:234
virtual bool train(ClassificationData trainingData)
Definition: MLBase.cpp:88
virtual bool loadModelFromFile(std::fstream &file)
Definition: KNN.cpp:529
virtual bool train_(ClassificationData &trainingData)
Definition: KNN.cpp:104
UINT distanceMethod
The number of neighbours to search for
Definition: KNN.h:233
bool setK(UINT K)
Definition: KNN.cpp:682
KNN(UINT K=10, bool useScaling=false, bool useNullRejection=false, Float nullRejectionCoeff=10.0, bool searchForBestKValue=false, UINT minKSearchValue=1, UINT maxKSearchValue=10)
ClassificationData trainingData
The maximum K value to end the search at
Definition: KNN.h:237
UINT maxKSearchValue
The minimum K value to start the search from
Definition: KNN.h:236
bool setDistanceMethod(UINT distanceMethod)
Definition: KNN.cpp:714
virtual bool recomputeNullRejectionThresholds()
Definition: KNN.cpp:663
KNN & operator=(const KNN &rhs)
Definition: KNN.cpp:63
bool setMaxKSearchValue(UINT maxKSearchValue)
Definition: KNN.cpp:695
virtual bool saveModelToFile(std::fstream &file) const
Definition: KNN.cpp:476
bool enableBestKValueSearch(bool searchForBestKValue)
Definition: KNN.cpp:700
virtual bool predict_(VectorFloat &inputVector)
Definition: KNN.cpp:314
virtual bool deepCopyFrom(const Classifier *classifier)
Definition: KNN.cpp:81
UINT getDistanceMethod()
Definition: KNN.h:162
virtual bool saveModelToFile(std::string filename) const
Definition: MLBase.cpp:146
UINT minKSearchValue
Sets if the best K value should be searched for or if the model should be trained with K ...
Definition: KNN.h:235
static RegisterClassifierModule< KNN > registerModule
Holds the stddev of the max-class distance of the training data for each of classes ...
Definition: KNN.h:241
VectorFloat trainingMu
Holds the trainingData to perform the predictions
Definition: KNN.h:238
virtual bool loadModelFromFile(std::string filename)
Definition: MLBase.cpp:168
virtual bool train_(ClassificationData &trainingData)
Definition: MLBase.cpp:90
bool setNullRejectionCoeff(Float nullRejectionCoeff)
Definition: KNN.cpp:705
bool setMinKSearchValue(UINT minKSearchValue)
Definition: KNN.cpp:690
virtual bool clear()
Definition: KNN.cpp:463
virtual ~KNN(void)
Definition: KNN.cpp:59
Definition: KNN.h:51
bool loadLegacyModelFromFile(std::fstream &file)
Definition: KNN.cpp:758