[mlpack-git] master: Add test for Pelleg-Moore k-means clustering. (6ecd7c6)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 22:01:12 EST 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

>---------------------------------------------------------------

commit 6ecd7c6b45a9d10d5dbbfc5740bad42a1661b721
Author: Ryan Curtin <ryan at ratml.org>
Date:   Sun Oct 12 20:30:25 2014 +0000

    Add test for Pelleg-Moore k-means clustering.


>---------------------------------------------------------------

6ecd7c6b45a9d10d5dbbfc5740bad42a1661b721
 src/mlpack/tests/kmeans_test.cpp | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/src/mlpack/tests/kmeans_test.cpp b/src/mlpack/tests/kmeans_test.cpp
index ddf5a22..178bdb5 100644
--- a/src/mlpack/tests/kmeans_test.cpp
+++ b/src/mlpack/tests/kmeans_test.cpp
@@ -9,6 +9,7 @@
 #include <mlpack/methods/kmeans/refined_start.hpp>
 #include <mlpack/methods/kmeans/elkan_kmeans.hpp>
 #include <mlpack/methods/kmeans/hamerly_kmeans.hpp>
+#include <mlpack/methods/kmeans/pelleg_moore_kmeans.hpp>
 
 #include <boost/test/unit_test.hpp>
 #include "old_boost_test_definitions.hpp"
@@ -555,4 +556,38 @@ BOOST_AUTO_TEST_CASE(HamerlyTest)
   }
 }
 
+BOOST_AUTO_TEST_CASE(PellegMooreTest)
+{
+  const size_t trials = 5;
+
+  for (size_t t = 0; t < trials; ++t)
+  {
+    arma::mat dataset(10, 1000);
+    dataset.randu();
+
+    const size_t k = 5 * (t + 1);
+    arma::mat centroids(10, k);
+    centroids.randu();
+
+    // Make sure the Pelleg-Moore algorithm and the naive method return the same
+    // clusters.
+    arma::mat naiveCentroids(centroids);
+    KMeans<> km;
+    arma::Col<size_t> assignments;
+    km.Cluster(dataset, k, assignments, naiveCentroids, false, true);
+
+    KMeans<metric::EuclideanDistance, RandomPartition, MaxVarianceNewCluster,
+        PellegMooreKMeans> pellegMoore;
+    arma::Col<size_t> pmAssignments;
+    arma::mat pmCentroids(centroids);
+    pellegMoore.Cluster(dataset, k, pmAssignments, pmCentroids, false, true);
+
+    for (size_t i = 0; i < dataset.n_cols; ++i)
+      BOOST_REQUIRE_EQUAL(assignments[i], pmAssignments[i]);
+
+    for (size_t i = 0; i < centroids.n_elem; ++i)
+      BOOST_REQUIRE_CLOSE(naiveCentroids[i], pmCentroids[i], 1e-5);
+  }
+}
+
 BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-git mailing list