From ea5b4b0ec6018a878c0df4afc86819065cd44800 Mon Sep 17 00:00:00 2001 From: Prabhat Date: Tue, 6 Mar 2018 14:58:43 +0530 Subject: [PATCH] Kmeans fix --- src/mlpack/methods/kmeans/kmeans_main.cpp | 7 +++++-- src/mlpack/tests/main_tests/kmeans_test.cpp | 23 ++++++++------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/mlpack/methods/kmeans/kmeans_main.cpp b/src/mlpack/methods/kmeans/kmeans_main.cpp index 2f42213805..0a169285e2 100644 --- a/src/mlpack/methods/kmeans/kmeans_main.cpp +++ b/src/mlpack/methods/kmeans/kmeans_main.cpp @@ -174,7 +174,9 @@ static void mlpackMain() template void FindEmptyClusterPolicy(const InitialPartitionPolicy& ipp) { - RequireOnlyOnePassed({ "allow_empty_clusters", "kill_empty_clusters" }, true); + if (CLI::HasParam("allow_empty_clusters") || + CLI::HasParam("kill_empty_clusters")) + RequireOnlyOnePassed({ "allow_empty_clusters", "kill_empty_clusters" }, true); if (CLI::HasParam("allow_empty_clusters")) FindLloydStepType(ipp); @@ -242,7 +244,8 @@ void RunKMeans(const InitialPartitionPolicy& ipp) // Make sure we have an output file if we're not doing the work in-place. RequireAtLeastOnePassed({ "in_place", "output", "centroid" }, false, "no results will be saved"); - + if (!CLI::HasParam("input")) + Log::Fatal << "Must specify input"<< endl; // Load our dataset. arma::mat dataset = CLI::GetParam("input"); arma::mat centroids; diff --git a/src/mlpack/tests/main_tests/kmeans_test.cpp b/src/mlpack/tests/main_tests/kmeans_test.cpp index e55399d764..e5ca280a71 100644 --- a/src/mlpack/tests/main_tests/kmeans_test.cpp +++ b/src/mlpack/tests/main_tests/kmeans_test.cpp @@ -62,7 +62,7 @@ BOOST_AUTO_TEST_CASE(NonNegativeClustersTest) /** - * Checking that number of Clusters is less than number of points to be clustered + * Checking that initial centroids are provided if clusters are to be auto detected */ BOOST_AUTO_TEST_CASE(PointsLessThanClustersTest) { @@ -72,7 +72,7 @@ BOOST_AUTO_TEST_CASE(PointsLessThanClustersTest) arma::mat inputData = arma::randu(N, D); SetInputParam("input", std::move(inputData)); - SetInputParam("clusters", (int) 11); // Invalid + SetInputParam("clusters", (int) 0); // Invalid Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); @@ -139,7 +139,6 @@ BOOST_AUTO_TEST_CASE(KmClusteringSizeCheck) SetInputParam("input", std::move(inputData)); SetInputParam("clusters", c); - SetInputParam("allow_empty_clusters", false); mlpackMain(); @@ -165,7 +164,6 @@ BOOST_AUTO_TEST_CASE(KmClusteringSizeCheckLabelOnly) SetInputParam("input", std::move(inputData)); SetInputParam("clusters", c); SetInputParam("labels_only", true); - SetInputParam("allow_empty_clusters", false); mlpackMain(); @@ -185,15 +183,14 @@ BOOST_AUTO_TEST_CASE(KmClusteringEmptyClustersCheck) int iterations = 100; arma::mat inputData; - if (!data::Load("vc2.csv", inputData)) - BOOST_FAIL("Unable to load train dataset vc2.csv!"); + if (!data::Load("test_data_3_1000.csv", inputData)) + BOOST_FAIL("Unable to load train dataset test_data_3_1000.csv!"); arma::mat initCentroid = arma::randu(inputData.n_rows, c); SetInputParam("input", inputData); SetInputParam("clusters", c); SetInputParam("labels_only", true); SetInputParam("max_iterations", iterations); - SetInputParam("allow_empty_clusters", false); SetInputParam("initial_centroids", initCentroid); mlpackMain(); @@ -271,7 +268,6 @@ BOOST_AUTO_TEST_CASE(KmClusteringResultSizeCheck) SetInputParam("input", inputData); SetInputParam("clusters", c); SetInputParam("in_place", true); - SetInputParam("allow_empty_clusters", false); mlpackMain(); arma::mat processedInput = CLI::GetParam("output"); @@ -292,7 +288,6 @@ BOOST_AUTO_TEST_CASE(KmClustersNotDefined) BOOST_FAIL("Unable to load train dataset vc2.csv!"); SetInputParam("input", std::move(inputData)); - SetInputParam("allow_empty_clusters", false); Log::Fatal.ignoreInput = true; BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); @@ -315,9 +310,9 @@ BOOST_AUTO_TEST_CASE(AlgorithmsSimilarTest) SetInputParam("input", inputData); SetInputParam("clusters", c); SetInputParam("algorithm", std::move(algo)); - SetInputParam("allow_empty_clusters", false); SetInputParam("labels_only", true); SetInputParam("initial_centroids", initCentroid); + SetInputParam("allow_empty_clusters", false); mlpackMain(); @@ -333,9 +328,9 @@ BOOST_AUTO_TEST_CASE(AlgorithmsSimilarTest) SetInputParam("input", inputData); SetInputParam("clusters", c); SetInputParam("algorithm", std::move(algo)); - SetInputParam("allow_empty_clusters", false); SetInputParam("labels_only", true); SetInputParam("initial_centroids", initCentroid); + SetInputParam("allow_empty_clusters", false); mlpackMain(); @@ -351,9 +346,9 @@ BOOST_AUTO_TEST_CASE(AlgorithmsSimilarTest) SetInputParam("input", inputData); SetInputParam("clusters", c); SetInputParam("algorithm", std::move(algo)); - SetInputParam("allow_empty_clusters", false); SetInputParam("labels_only", true); SetInputParam("initial_centroids", initCentroid); + SetInputParam("allow_empty_clusters", false); mlpackMain(); @@ -369,9 +364,9 @@ BOOST_AUTO_TEST_CASE(AlgorithmsSimilarTest) SetInputParam("input", inputData); SetInputParam("clusters", c); SetInputParam("algorithm", std::move(algo)); - SetInputParam("allow_empty_clusters", false); SetInputParam("labels_only", true); SetInputParam("initial_centroids", initCentroid); + SetInputParam("allow_empty_clusters", false); mlpackMain(); @@ -387,9 +382,9 @@ BOOST_AUTO_TEST_CASE(AlgorithmsSimilarTest) SetInputParam("input", std::move(inputData)); SetInputParam("clusters", c); SetInputParam("algorithm", std::move(algo)); - SetInputParam("allow_empty_clusters", false); SetInputParam("labels_only", true); SetInputParam("initial_centroids", std::move(initCentroid)); + SetInputParam("allow_empty_clusters", false); mlpackMain();