RapidLib  v2.2.0
A simple library for interactive machine learning
knnClassification.h
Go to the documentation of this file.
1 //
2 // knnClassification.h
3 // RapidLib
4 //
5 // Created by mzed on 05/09/2016.
6 // Copyright © 2016 Goldsmiths. All rights reserved.
7 //
8 
9 #ifndef KNNCLASSIFICATION_H
10 #define KNNCLASSIFICATION_H
11 
12 #include <vector>
13 #include "baseModel.h"
14 
15 #ifndef EMSCRIPTEN
16 #include "../dependencies/json/json.h"
17 #endif
18 
20 template<typename T>
21 class knnClassification final : public baseModel<T>
22 {
23 public:
30  knnClassification(const int &num_inputs,
31  const std::vector<size_t> &which_inputs,
32  const std::vector<trainingExampleTemplate<T> > &trainingSet,
33  const int k);
35 
40  void addNeighbour(const int &classNum, const std::vector<T> &features);
41 
46  T run(const std::vector<T> &inputVector) override;
47 
53  void train(const std::vector<trainingExampleTemplate<T> > &trainingSet) override;
54 
60  void train(const std::vector<trainingExampleTemplate<T> >& trainingSet, const std::size_t whichOutput) override;
61 
63  void reset() override;
64 
68  size_t getNumInputs() const override;
69 
73  std::vector<size_t> getWhichInputs() const override;
74 
76  int getK() const;
80  void setK(int newK);
81 
82 #ifndef EMSCRIPTEN
86  void getJSONDescription(Json::Value &currentModel) override;
87 #endif
88 
89 private:
90  int numInputs;
91  std::vector<size_t> whichInputs;
92  std::size_t whichOutput;
93  std::vector<trainingExampleTemplate<T>> neighbours;
94  int desiredK; //K that user asked for might be limited but number of examples
95  int currentK; //K minimum of desiredK or neighbours.size()
96  inline void updateK();
97 };
98 
99 #endif
Definition: baseModel.h:42
Definition: knnClassification.h:22
std::vector< size_t > getWhichInputs() const override
Definition: knnClassification.cpp:49
void getJSONDescription(Json::Value &currentModel) override
Definition: knnClassification.cpp:185
void reset() override
Definition: knnClassification.cpp:37
int getK() const
Definition: knnClassification.cpp:55
~knnClassification()
Definition: knnClassification.cpp:34
void addNeighbour(const int &classNum, const std::vector< T > &features)
Definition: knnClassification.cpp:74
void setK(int newK)
Definition: knnClassification.cpp:67
size_t getNumInputs() const override
Definition: knnClassification.cpp:43
T run(const std::vector< T > &inputVector) override
Definition: knnClassification.cpp:100
void train(const std::vector< trainingExampleTemplate< T > > &trainingSet) override
Definition: knnClassification.cpp:84
knnClassification(const int &num_inputs, const std::vector< size_t > &which_inputs, const std::vector< trainingExampleTemplate< T > > &trainingSet, const int k)
Definition: knnClassification.cpp:20
Definition: trainingExample.h:19