13 #ifndef MLPACK_METHODS_KMEANS_PELLEG_MOORE_KMEANS_IMPL_HPP 14 #define MLPACK_METHODS_KMEANS_PELLEG_MOORE_KMEANS_IMPL_HPP 22 template<
typename MetricType,
typename MatType>
24 const MatType& dataset,
27 tree(new
TreeType(const_cast<MatType&>(datasetOrig))),
28 dataset(tree->Dataset()),
30 distanceCalculations(0)
35 template<
typename MetricType,
typename MatType>
43 template<
typename MetricType,
typename MatType>
45 const arma::mat& centroids,
46 arma::mat& newCentroids,
47 arma::Col<size_t>& counts)
49 newCentroids.zeros(centroids.n_rows, centroids.n_cols);
50 counts.zeros(centroids.n_cols);
54 RulesType rules(dataset, centroids, newCentroids, counts, metric);
57 typename TreeType::template SingleTreeTraverser<RulesType> traverser(rules);
61 traverser.Traverse(0, *tree);
63 distanceCalculations += rules.DistanceCalculations();
66 double residual = 0.0;
67 for (
size_t c = 0; c < centroids.n_cols; ++c)
71 newCentroids.col(c) /= counts(c);
72 residual += std::pow(metric.Evaluate(centroids.col(c),
73 newCentroids.col(c)), 2.0);
76 distanceCalculations += centroids.n_cols;
78 return std::sqrt(residual);
double Iterate(const arma::mat ¢roids, arma::mat &newCentroids, arma::Col< size_t > &counts)
Run a single iteration of the Pelleg-Moore blacklist algorithm, updating the given centroids into the...
Definition: pelleg_moore_kmeans_impl.hpp:44
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
A binary space partitioning tree, such as a KD-tree or a ball tree.
Definition: binary_space_tree.hpp:54
~PellegMooreKMeans()
Delete the tree constructed by the PellegMooreKMeans object.
Definition: pelleg_moore_kmeans_impl.hpp:36
PellegMooreKMeans(const MatType &dataset, MetricType &metric)
Construct the PellegMooreKMeans object, which must construct a tree.
Definition: pelleg_moore_kmeans_impl.hpp:23
The rules class for the single-tree Pelleg-Moore kd-tree traversal for k-means clustering.
Definition: pelleg_moore_kmeans_rules.hpp:33