TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
machine_learning::k_nearest_neighbors::Knn Class Reference

K-Nearest Neighbors (Knn) class using Euclidean distance as distance metric. More...

Collaboration diagram for machine_learning::k_nearest_neighbors::Knn:
[legend]

Public Member Functions

 Knn (std::vector< std::vector< double > > &X, std::vector< int > &Y)
 Construct a new Knn object.
 
 Knn (const Knn &model)=default
 
Knnoperator= (const Knn &model)=default
 
 Knn (Knn &&)=default
 
Knnoperator= (Knn &&)=default
 
 ~Knn ()=default
 Destroy the Knn object.
 
int predict (std::vector< double > &sample, int k)
 Classify sample.
 

Private Attributes

std::vector< std::vector< double > > X_ {}
 attributes vector
 
std::vector< int > Y_ {}
 labels vector
 

Detailed Description

K-Nearest Neighbors (Knn) class using Euclidean distance as distance metric.

Definition at line 55 of file k_nearest_neighbors.cpp.

Constructor & Destructor Documentation

◆ Knn() [1/3]

machine_learning::k_nearest_neighbors::Knn::Knn ( std::vector< std::vector< double > > & X,
std::vector< int > & Y )
inlineexplicit

Construct a new Knn object.

Using lazy-learning approch, just holds in memory the dataset.

Parameters
Xattributes vector
Ylabels vector

Definition at line 67 of file k_nearest_neighbors.cpp.

68 : X_(X), Y_(Y){};
std::vector< std::vector< double > > X_
attributes vector

◆ Knn() [2/3]

machine_learning::k_nearest_neighbors::Knn::Knn ( const Knn & model)
default

Copy Constructor for class Knn.

Parameters
modelinstance of class to be copied

◆ Knn() [3/3]

machine_learning::k_nearest_neighbors::Knn::Knn ( Knn && )
default

Move constructor for class Knn

Member Function Documentation

◆ operator=() [1/2]

Knn & machine_learning::k_nearest_neighbors::Knn::operator= ( const Knn & model)
default

Copy assignment operator for class Knn

◆ operator=() [2/2]

Knn & machine_learning::k_nearest_neighbors::Knn::operator= ( Knn && )
default

Move assignment operator for class Knn

◆ predict()

int machine_learning::k_nearest_neighbors::Knn::predict ( std::vector< double > & sample,
int k )
inline

Classify sample.

Parameters
samplesample
knumber of neighbors
Returns
int label of most frequent neighbors

Definition at line 103 of file k_nearest_neighbors.cpp.

103 {
104 std::vector<int> neighbors;
105 std::vector<std::pair<double, int>> distances;
106 for (size_t i = 0; i < this->X_.size(); ++i) {
107 auto current = this->X_.at(i);
108 auto label = this->Y_.at(i);
109 auto distance = euclidean_distance(current, sample);
110 distances.emplace_back(distance, label);
111 }
112 std::sort(distances.begin(), distances.end());
113 for (int i = 0; i < k; i++) {
114 auto label = distances.at(i).second;
115 neighbors.push_back(label);
116 }
117 std::unordered_map<int, int> frequency;
118 for (auto neighbor : neighbors) {
119 ++frequency[neighbor];
120 }
121 std::pair<int, int> predicted;
122 predicted.first = -1;
123 predicted.second = -1;
124 for (auto& kv : frequency) {
125 if (kv.second > predicted.second) {
126 predicted.second = kv.second;
127 predicted.first = kv.first;
128 }
129 }
130 return predicted.first;
131 }
double k(double x)
Another test function.
double euclidean_distance(const std::vector< T > &a, const std::vector< T > &b)
Compute the Euclidean distance between two vectors.

Member Data Documentation

◆ X_

std::vector<std::vector<double> > machine_learning::k_nearest_neighbors::Knn::X_ {}
private

attributes vector

Definition at line 57 of file k_nearest_neighbors.cpp.

57{};

◆ Y_

std::vector<int> machine_learning::k_nearest_neighbors::Knn::Y_ {}
private

labels vector

Definition at line 58 of file k_nearest_neighbors.cpp.

58{};

The documentation for this class was generated from the following file: