Represents a K Nearest Neighbors (KNN) classifier.
More...
#include <knn.hpp>
|
| KNN () |
| Constructor for the KNN class. More...
|
|
| ~KNN () |
| Destructor for the KNN class. More...
|
|
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. More...
|
|
int | predict (const std::vector< double > &input_vector, int k) |
| Predicts the label of a given input vector using KNN algorithm. More...
|
|
|
double | calculateEuclideanDistance (const std::vector< double > &vec1, const std::vector< double > &vec2) |
| Calculates the Euclidean distance between two vectors. More...
|
|
Represents a K Nearest Neighbors (KNN) classifier.
Definition at line 52 of file knn.hpp.
◆ KNN()
Constructor for the KNN class.
Definition at line 39 of file knn.cpp.
◆ ~KNN()
Destructor for the KNN class.
Definition at line 42 of file knn.cpp.
◆ calculateEuclideanDistance()
double gpmp::ml::KNN::calculateEuclideanDistance |
( |
const std::vector< double > & |
vec1, |
|
|
const std::vector< double > & |
vec2 |
|
) |
| |
|
private |
Calculates the Euclidean distance between two vectors.
- Parameters
-
vec1 | The first vector |
vec2 | The second vector |
- Returns
- The Euclidean distance between the two vectors
Definition at line 107 of file knn.cpp.
109 double distance = 0.0;
110 for (
size_t i = 0; i < vec1.size(); ++i) {
111 double diff = vec1[i] - vec2[i];
112 distance += diff * diff;
114 return std::sqrt(distance);
◆ predict()
int gpmp::ml::KNN::predict |
( |
const std::vector< double > & |
input_vector, |
|
|
int |
k |
|
) |
| |
Predicts the label of a given input vector using KNN algorithm.
- Parameters
-
input_vector | The input vector for which prediction is to be made |
k | The number of nearest neighbors to consider |
- Returns
- The predicted label
Definition at line 56 of file knn.cpp.
58 throw std::logic_error(
59 "Model not trained. Call train() before predict.");
63 throw std::invalid_argument(
"Invalid input vector size.");
67 if (k <= 0 ||
static_cast<size_t>(k) >
training_data.size()) {
68 throw std::invalid_argument(
"Invalid value of k.");
72 std::vector<std::pair<double, int>> distances;
76 distances.emplace_back(distance,
labels[i]);
83 [](
const std::pair<double, int> &a,
const std::pair<double, int> &b) {
84 return a.first < b.first;
88 std::unordered_map<int, int> label_counts;
89 for (
int i = 0; i < k; ++i) {
90 label_counts[distances[i].second]++;
95 int predicted_label = -1;
96 for (
const auto &entry : label_counts) {
97 if (entry.second > max_votes) {
98 max_votes = entry.second;
99 predicted_label = entry.first;
103 return predicted_label;
std::vector< std::vector< double > > training_data
std::vector< int > labels
double calculateEuclideanDistance(const std::vector< double > &vec1, const std::vector< double > &vec2)
Calculates the Euclidean distance between two vectors.
◆ train()
void gpmp::ml::KNN::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.
- Parameters
-
training_data | The training data, a vector of feature vectors |
labels | The corresponding labels for the training data |
Definition at line 45 of file knn.cpp.
48 if (_training_data.size() != _labels.size() || _training_data.empty()) {
49 throw std::invalid_argument(
"Invalid training data or labels.");
◆ labels
std::vector<int> gpmp::ml::KNN::labels |
|
private |
◆ training_data
std::vector<std::vector<double> > gpmp::ml::KNN::training_data |
|
private |
< The training data stored as feature vectors The corresponding labels for the training data
Definition at line 82 of file knn.hpp.
The documentation for this class was generated from the following files: