TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
k_nearest_neighbors.cpp File Reference

Implementation of [K-Nearest Neighbors algorithm] (https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm). More...

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <numeric>
#include <unordered_map>
#include <vector>
Include dependency graph for k_nearest_neighbors.cpp:

Go to the source code of this file.

Classes

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

Namespaces

namespace  machine_learning
 A* search algorithm
 
namespace  k_nearest_neighbors
 Functions for the [K-Nearest Neighbors algorithm] (https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm) implementation.
 

Functions

template<typename T >
double machine_learning::k_nearest_neighbors::euclidean_distance (const std::vector< T > &a, const std::vector< T > &b)
 Compute the Euclidean distance between two vectors.
 
static void test ()
 Self-test implementations.
 
int main (int argc, char *argv[])
 Main function.
 

Detailed Description

Implementation of [K-Nearest Neighbors algorithm] (https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm).

Author
Luiz Carlos Cosmi Filho

K-nearest neighbors algorithm, also known as KNN or k-NN, is a supervised learning classifier, which uses proximity to make classifications. This implementantion uses the Euclidean Distance as distance metric to find the K-nearest neighbors.

Definition in file k_nearest_neighbors.cpp.

Function Documentation

◆ euclidean_distance()

template<typename T >
double machine_learning::k_nearest_neighbors::euclidean_distance ( const std::vector< T > & a,
const std::vector< T > & b )

Compute the Euclidean distance between two vectors.

Template Parameters
Ttypename of the vector
Parameters
afirst unidimentional vector
bsecond unidimentional vector
Returns
double scalar representing the Euclidean distance between provided vectors

Definition at line 43 of file k_nearest_neighbors.cpp.

43 {
44 std::vector<double> aux;
45 std::transform(a.begin(), a.end(), b.begin(), std::back_inserter(aux),
46 [](T x1, T x2) { return std::pow((x1 - x2), 2); });
47 aux.shrink_to_fit();
48 return std::sqrt(std::accumulate(aux.begin(), aux.end(), 0.0));
49}

◆ main()

int main ( int argc,
char * argv[] )

Main function.

Parameters
argccommandline argument count (ignored)
argvcommandline array of arguments (ignored)
Returns
int 0 on exit

Definition at line 193 of file k_nearest_neighbors.cpp.

193 {
194 test(); // run self-test implementations
195 return 0;
196}
static void test()
Self-test implementations.

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 140 of file k_nearest_neighbors.cpp.

140 {
141 std::cout << "------- Test 1 -------" << std::endl;
142 std::vector<std::vector<double>> X1 = {{0.0, 0.0}, {0.25, 0.25},
143 {0.0, 0.5}, {0.5, 0.5},
144 {1.0, 0.5}, {1.0, 1.0}};
145 std::vector<int> Y1 = {1, 1, 1, 1, 2, 2};
146 auto model1 = machine_learning::k_nearest_neighbors::Knn(X1, Y1);
147 std::vector<double> sample1 = {1.2, 1.2};
148 std::vector<double> sample2 = {0.1, 0.1};
149 std::vector<double> sample3 = {0.1, 0.5};
150 std::vector<double> sample4 = {1.0, 0.75};
151 assert(model1.predict(sample1, 2) == 2);
152 assert(model1.predict(sample2, 2) == 1);
153 assert(model1.predict(sample3, 2) == 1);
154 assert(model1.predict(sample4, 2) == 2);
155 std::cout << "... Passed" << std::endl;
156 std::cout << "------- Test 2 -------" << std::endl;
157 std::vector<std::vector<double>> X2 = {
158 {0.0, 0.0, 0.0}, {0.25, 0.25, 0.0}, {0.0, 0.5, 0.0}, {0.5, 0.5, 0.0},
159 {1.0, 0.5, 0.0}, {1.0, 1.0, 0.0}, {1.0, 1.0, 1.0}, {1.5, 1.5, 1.0}};
160 std::vector<int> Y2 = {1, 1, 1, 1, 2, 2, 3, 3};
161 auto model2 = machine_learning::k_nearest_neighbors::Knn(X2, Y2);
162 std::vector<double> sample5 = {1.2, 1.2, 0.0};
163 std::vector<double> sample6 = {0.1, 0.1, 0.0};
164 std::vector<double> sample7 = {0.1, 0.5, 0.0};
165 std::vector<double> sample8 = {1.0, 0.75, 1.0};
166 assert(model2.predict(sample5, 2) == 2);
167 assert(model2.predict(sample6, 2) == 1);
168 assert(model2.predict(sample7, 2) == 1);
169 assert(model2.predict(sample8, 2) == 3);
170 std::cout << "... Passed" << std::endl;
171 std::cout << "------- Test 3 -------" << std::endl;
172 std::vector<std::vector<double>> X3 = {{0.0}, {1.0}, {2.0}, {3.0},
173 {4.0}, {5.0}, {6.0}, {7.0}};
174 std::vector<int> Y3 = {1, 1, 1, 1, 2, 2, 2, 2};
175 auto model3 = machine_learning::k_nearest_neighbors::Knn(X3, Y3);
176 std::vector<double> sample9 = {0.5};
177 std::vector<double> sample10 = {2.9};
178 std::vector<double> sample11 = {5.5};
179 std::vector<double> sample12 = {7.5};
180 assert(model3.predict(sample9, 3) == 1);
181 assert(model3.predict(sample10, 3) == 1);
182 assert(model3.predict(sample11, 3) == 2);
183 assert(model3.predict(sample12, 3) == 2);
184 std::cout << "... Passed" << std::endl;
185}
K-Nearest Neighbors (Knn) class using Euclidean distance as distance metric.