[mlpack] 74/207: Add a test of actual clustering quality.

Barak A. Pearlmutter barak+git at pearlmutter.net
Thu Mar 23 17:53:42 UTC 2017


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch master
in repository mlpack.

commit 87d85a3d952a38bd74a2acfaead8c88480b9f671
Author: Ryan Curtin <ryan at ratml.org>
Date:   Thu Aug 4 16:19:42 2016 -0400

    Add a test of actual clustering quality.
---
 src/mlpack/tests/dbscan_test.cpp | 65 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/src/mlpack/tests/dbscan_test.cpp b/src/mlpack/tests/dbscan_test.cpp
index 94bccda..d9ab785 100644
--- a/src/mlpack/tests/dbscan_test.cpp
+++ b/src/mlpack/tests/dbscan_test.cpp
@@ -12,6 +12,7 @@
 
 using namespace mlpack;
 using namespace mlpack::dbscan;
+using namespace mlpack::distribution;
 
 BOOST_AUTO_TEST_SUITE(DBSCANTest);
 
@@ -74,4 +75,68 @@ BOOST_AUTO_TEST_CASE(OutlierTest)
   BOOST_REQUIRE_EQUAL(assignments[101], SIZE_MAX);
 }
 
+/**
+ * Check that the Gaussian clusters are correctly found.
+ */
+BOOST_AUTO_TEST_CASE(GaussiansTest)
+{
+  arma::mat points(3, 300);
+
+  GaussianDistribution g1(3), g2(3), g3(3);
+  g1.Mean() = arma::vec("0.0 0.0 0.0");
+  g2.Mean() = arma::vec("6.0 6.0 8.0");
+  g3.Mean() = arma::vec("-6.0 1.0 -7.0");
+  for (size_t i = 0; i < 100; ++i)
+    points.col(i) = g1.Random();
+  for (size_t i = 100; i < 200; ++i)
+    points.col(i) = g2.Random();
+  for (size_t i = 200; i < 300; ++i)
+    points.col(i) = g3.Random();
+
+  DBSCAN<> d(1.0, 3);
+
+  arma::Row<size_t> assignments;
+  arma::mat centroids;
+  const size_t clusters = d.Cluster(points, assignments, centroids);
+  BOOST_REQUIRE_EQUAL(clusters, 3);
+
+  // Our centroids should be close to one of our Gaussians.
+  arma::Row<size_t> matches(3);
+  matches.fill(3);
+  for (size_t j = 0; j < 3; ++j)
+  {
+    if (arma::norm(g1.Mean() - centroids.col(j)) < 1.0)
+      matches(j) = 0;
+    else if (arma::norm(g2.Mean() - centroids.col(j)) < 1.0)
+      matches(j) = 1;
+    else if (arma::norm(g3.Mean() - centroids.col(j)) < 1.0)
+      matches(j) = 2;
+
+    BOOST_REQUIRE_NE(matches(j), 3);
+  }
+
+  BOOST_REQUIRE_NE(matches(0), matches(1));
+  BOOST_REQUIRE_NE(matches(1), matches(2));
+  BOOST_REQUIRE_NE(matches(2), matches(0));
+
+  for (size_t i = 0; i < 100; ++i)
+  {
+    // Each point should either be noise or in cluster matches(0).
+    BOOST_REQUIRE_NE(assignments(i), matches(1));
+    BOOST_REQUIRE_NE(assignments(i), matches(2));
+  }
+
+  for (size_t i = 100; i < 200; ++i)
+  {
+    BOOST_REQUIRE_NE(assignments(i), matches(0));
+    BOOST_REQUIRE_NE(assignments(i), matches(2));
+  }
+
+  for (size_t i = 200; i < 300; ++i)
+  {
+    BOOST_REQUIRE_NE(assignments(i), matches(0));
+    BOOST_REQUIRE_NE(assignments(i), matches(1));
+  }
+}
+
 BOOST_AUTO_TEST_SUITE_END();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git



More information about the debian-science-commits mailing list