[mlpack-svn] r14917 - mlpack/trunk/src/mlpack/methods/kmeans

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Apr 18 14:28:41 EDT 2013


Author: rcurtin
Date: 2013-04-18 14:28:41 -0400 (Thu, 18 Apr 2013)
New Revision: 14917

Modified:
   mlpack/trunk/src/mlpack/methods/kmeans/kmeans_impl.hpp
Log:
Better error checking, and, actually *use* the initial guesses if they were
given to us.  Logic is hard...


Modified: mlpack/trunk/src/mlpack/methods/kmeans/kmeans_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/kmeans/kmeans_impl.hpp	2013-04-18 18:18:53 UTC (rev 14916)
+++ mlpack/trunk/src/mlpack/methods/kmeans/kmeans_impl.hpp	2013-04-18 18:28:41 UTC (rev 14917)
@@ -539,34 +539,28 @@
   }
 
   // Now, the initial assignments.  First determine if they are necessary.
-  if (initialAssignmentGuess && assignments.n_elem != data.n_cols)
+  if (initialAssignmentGuess)
   {
-    Log::Fatal << "KMeans::Cluster(): initial cluster assignments (length "
-        << assignments.n_elem << ") not the same size as the dataset (size "
-        << data.n_cols << ")!" << std::endl;
+    if (assignments.n_elem != data.n_cols)
+      Log::Fatal << "KMeans::Cluster(): initial cluster assignments (length "
+          << assignments.n_elem << ") not the same size as the dataset (size "
+          << data.n_cols << ")!" << std::endl;
   }
-  else if (initialCentroidGuess && (centroids.n_rows != data.n_rows ||
-                                    centroids.n_cols != clusters))
+  else if (initialCentroidGuess)
   {
-    Log::Fatal << "KMeans::Cluster(): wrong number of initial cluster centroids"
-        << " (" << centroids.n_cols << ", should be " << clusters << ") or "
-        << "wrong dimensionality (" << centroids.n_rows << ", should be "
+    if (centroids.n_cols != clusters)
+      Log::Fatal << "KMeans::Cluster(): wrong number of initial cluster "
+        << "centroids (" << centroids.n_cols << ", should be " << clusters
+        << ")!" << std::endl;
+
+    if (centroids.n_rows != data.n_rows)
+      Log::Fatal << "KMeans::Cluster(): initial cluster centroids have wrong "
+        << " dimensionality (" << centroids.n_rows << ", should be "
         << data.n_rows << ")!" << std::endl;
-  }
-  else
-  {
-    // Use the partitioner to come up with the partition assignments.
-    partitioner.Cluster(data, actualClusters, assignments);
-  }
 
-  // Counts of points in each cluster.
-  arma::Col<size_t> counts(actualClusters);
-  counts.zeros();
-
-  // If we received an initial cluster guess, assign the points for the first
-  // time.  Note that initialAssignmentGuess supersedes initialCentroidGuess.
-  if (initialCentroidGuess && !initialAssignmentGuess)
-  {
+    // If there were no problems, construct the initial assignments from the
+    // given centroids.
+    assignments.set_size(data.n_cols);
     for (size_t i = 0; i < data.n_cols; ++i)
     {
       // Find the closest centroid to this point.
@@ -588,7 +582,16 @@
       assignments[i] = closestCluster;
     }
   }
+  else
+  {
+    // Use the partitioner to come up with the partition assignments.
+    partitioner.Cluster(data, actualClusters, assignments);
+  }
 
+  // Counts of points in each cluster.
+  arma::Col<size_t> counts(actualClusters);
+  counts.zeros();
+
   // Resize to correct size.
   centroids.set_size(data.n_rows, actualClusters);
 




More information about the mlpack-svn mailing list