Repository: TheAlgorithms/C-Plus-Plus
File: machine_learning/k_nearest_neighbors.cpp (C++)

Code before:
 * @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 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.
 */

#include <algorithm>      /// for std::transform and std::sort
#include <cassert>        /// for assert
#include <cmath>          /// for std::pow and std::sqrt
#include <iostream>       /// for std::cout
#include <numeric>        /// for std::accumulate
#include <unordered_map>  /// for std::unordered_map
#include <vector>         /// 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 <typename T>
double euclidean_distance(const std::vector<T>& a, const std::vector<T>& b) {
    std::vector<double> 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) class using Euclidean distance as
 * distance metric.
 */

Review comment (suggestion):
```suggestion
 * @brief Functions for the [K-Nearest Neighbors algorithm]
 * (https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm) implementation
```


Code after:
 * @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 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.
 */

#include <algorithm>      /// for std::transform and std::sort
#include <cassert>        /// for assert
#include <cmath>          /// for std::pow and std::sqrt
#include <iostream>       /// for std::cout
#include <numeric>        /// for std::accumulate
#include <unordered_map>  /// for std::unordered_map
#include <vector>         /// for std::vector

/**
 * @namespace machine_learning
 * @brief Machine learning algorithms
 */
namespace machine_learning {

/**
 * @namespace k_nearest_neighbors
 * @brief Functions for the [K-Nearest Neighbors algorithm]
 * (https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm) implementation
 */
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 <typename T>
double euclidean_distance(const std::vector<T>& a, const std::vector<T>& b) {
    std::vector<double> 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) class using Euclidean distance as
 * distance metric.