diff --git a/machine_learning/k_nearest_neighbors.cpp b/machine_learning/k_nearest_neighbors.cpp new file mode 100644 index 000000000..75dac0357 --- /dev/null +++ b/machine_learning/k_nearest_neighbors.cpp @@ -0,0 +1,182 @@ +/** + * @file + * @brief Implementation of [K-Nearest Neighbors algorithm] + * (https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm). + * @author [Luiz Carlos Cosmi Filho](https://github.com/luizcarloscf) + * @details + * Program that implements the k-nearest neighbors algorithm, also known as KNN + * or k-NN, a supervised learning classifier, which uses proximity to make + * classifications. This implementantion uses the Euclidean Distance to find the + * K-nearest neighbors. + */ + +#include // for std::transform and std::sort +#include // for assert +#include // for std::pow and std::sqrt +#include // for std::cout +#include // for std::accumulate +#include // for std::unordered_map +#include // for std::vector + +/** + * @namespace machine_learning + * @brief Machine learning algorithms + */ +namespace machine_learning { + +/** + * @namespace k_nearest_neighbors + * @brief K-nearest neighbors algorithm + */ +namespace k_nearest_neighbors { + +/** + * @brief Compute the Euclidean distance between two vectors. + * + * @tparam T typename of the vector + * @param a First unidimentional vector + * @param b Second unidimentional vector + * @return double scalar representing the Euclidean distance between provided + * vectors. + */ +template +double euclidean_distance(const std::vector& a, const std::vector& b) { + std::vector aux; + std::transform(a.begin(), a.end(), b.begin(), std::back_inserter(aux), + [](T x1, T x2) { return std::pow((x1 - x2), 2); }); + aux.shrink_to_fit(); + return std::sqrt(std::accumulate(aux.begin(), aux.end(), 0.0)); +} + +/** + * @brief K-Nearest Neighbors (Knn) implementation using Euclidean distance as + * metric distance. + */ +class Knn { + private: + std::vector> X_{}; ///< attributes vector + std::vector Y_{}; ///< labels vector + + public: + /** + * @brief Construct a new Knn object. + * @details Using lazy-learning approch, just holds in memory the dataset. + * @param X Attributes vector + * @param Y Labels vector + */ + explicit Knn(std::vector>& X, std::vector& Y) + : X_(X), Y_(Y){}; + /** + * Copy Constructor for class Knn. + * + * @param model instance of class to be copied. + */ + Knn(const Knn& model) = default; + + /** + * Copy assignment operator for class NeuralNetwork + */ + Knn& operator=(const Knn& model) = default; + + /** + * Move constructor for class NeuralNetwork + */ + Knn(Knn&&) = default; + + /** + * Move assignment operator for class NeuralNetwork + */ + Knn& operator=(Knn&&) = default; + + /** + * @brief Destroy the Knn object + */ + ~Knn() = default; + /** + * @brief Classify sample. + * + * @param sample Sample + * @param k Number of neighbor + * @return int Most frequent neighbor label + */ + int predict(std::vector& sample, int k) { + std::vector neighbors; + std::vector>> points; + std::vector> distances; + for (size_t i = 0; i < this->X_.size(); ++i) { + auto current = this->X_.at(i); + auto label = this->Y_.at(i); + auto distance = euclidean_distance(current, sample); + distances.push_back(std::pair(distance, label)); + } + std::sort(distances.begin(), distances.end()); + for (int i = 0; i < k; i++) { + auto label = distances.at(i).second; + neighbors.push_back(label); + } + std::unordered_map frequency; + // building frequency of each neighbor + for (auto neighbor : neighbors) { + ++frequency[neighbor]; + } + std::pair predicted; + predicted.first = -1; + predicted.second = -1; + for (auto& kv : frequency) { + if (kv.second > predicted.second) { + predicted.second = kv.second; + predicted.first = kv.first; + } + } + return predicted.first; + } +}; +} // namespace k_nearest_neighbors +} // namespace machine_learning + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + std::vector> X1 = {{0.0, 0.0}, {0.25, 0.25}, + {0.0, 0.5}, {0.5, 0.5}, + {1.0, 0.5}, {1.0, 1.0}}; + std::vector Y1 = {1, 1, 1, 1, 2, 2}; + auto model1 = machine_learning::k_nearest_neighbors::Knn(X1, Y1); + std::vector sample1 = {1.2, 1.2}; + std::vector sample2 = {0.1, 0.1}; + std::vector sample3 = {0.1, 0.5}; + std::vector sample4 = {1.0, 0.75}; + + assert(model1.predict(sample1, 2) == 2); + assert(model1.predict(sample2, 2) == 1); + assert(model1.predict(sample3, 2) == 1); + assert(model1.predict(sample4, 2) == 2); + + std::vector> X2 = { + {0.0, 0.0, 0.0}, {0.25, 0.25, 0.0}, {0.0, 0.5, 0.0}, {0.5, 0.5, 0.0}, + {1.0, 0.5, 0.0}, {1.0, 1.0, 0.0}, {1.0, 1.0, 1.0}, {1.5, 1.5, 1.0}}; + std::vector Y2 = {1, 1, 1, 1, 2, 2, 3, 3}; + auto model2 = machine_learning::k_nearest_neighbors::Knn(X2, Y2); + std::vector sample5 = {1.2, 1.2, 0.0}; + std::vector sample6 = {0.1, 0.1, 0.0}; + std::vector sample7 = {0.1, 0.5, 0.0}; + std::vector sample8 = {1.0, 0.75, 1.0}; + + assert(model2.predict(sample5, 2) == 2); + assert(model2.predict(sample6, 2) == 1); + assert(model2.predict(sample7, 2) == 1); + assert(model2.predict(sample8, 2) == 3); +} + +/** + * @brief Main function + * @param argc commandline argument count (ignored) + * @param argv commandline array of arguments (ignored) + * @return int + */ +int main(int argc, char* argv[]) { + test(); // run self-test implementations + return 0; +}