openGPMP
Open Source Mathematics Package
knn.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 
39 #ifndef KNN_hpp
40 #define KNN_hpp
41 
42 #include <vector>
43 
44 namespace gpmp {
45 
46 namespace ml {
47 
52 class KNN {
53  public:
57  KNN();
58 
62  ~KNN();
63 
69  void train(const std::vector<std::vector<double>> &training_data,
70  const std::vector<int> &labels);
71 
78  int predict(const std::vector<double> &input_vector, int k);
79 
80  private:
82  std::vector<std::vector<double>> training_data;
84  std::vector<int> labels;
85 
92  double calculateEuclideanDistance(const std::vector<double> &vec1,
93  const std::vector<double> &vec2);
94 };
95 
96 } // namespace ml
97 
98 } // namespace gpmp
99 
100 #endif
Represents a K Nearest Neighbors (KNN) classifier.
Definition: knn.hpp:52
std::vector< std::vector< double > > training_data
Definition: knn.hpp:82
int predict(const std::vector< double > &input_vector, int k)
Predicts the label of a given input vector using KNN algorithm.
Definition: knn.cpp:56
std::vector< int > labels
Definition: knn.hpp:84
double calculateEuclideanDistance(const std::vector< double > &vec1, const std::vector< double > &vec2)
Calculates the Euclidean distance between two vectors.
Definition: knn.cpp:107
void train(const std::vector< std::vector< double >> &training_data, const std::vector< int > &labels)
Trains the KNN model with the given training data and labels.
Definition: knn.cpp:45
~KNN()
Destructor for the KNN class.
Definition: knn.cpp:42
KNN()
Constructor for the KNN class.
Definition: knn.cpp:39
The source C++ openGPMP namespace.