Skip to content

Commit

Permalink
Add a test case for random initialization.
Browse files Browse the repository at this point in the history
Also workaround a bug where double[][] class cast fails
  • Loading branch information
shivaram committed Aug 6, 2013
1 parent 471fbad commit 6caec3f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mllib/src/main/scala/spark/mllib/clustering/KMeans.scala
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ class KMeans private (
*/
private def initRandom(data: RDD[Array[Double]]): Array[ClusterCenters] = {
// Sample all the cluster centers in one pass to avoid repeated scans
val sample = data.takeSample(true, runs * k, new Random().nextInt())
Array.tabulate(runs)(r => sample.slice(r * k, (r + 1) * k))
val sample = data.takeSample(true, runs * k, new Random().nextInt()).toSeq
Array.tabulate(runs)(r => sample.slice(r * k, (r + 1) * k).toArray)
}

/**
Expand Down
11 changes: 11 additions & 0 deletions mllib/src/test/scala/spark/mllib/clustering/JavaKMeansSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public void runKMeansUsingStaticMethods() {

JavaRDD<double[]> data = sc.parallelize(points, 2);
KMeansModel model = KMeans.train(data.rdd(), 1, 1);
assertSetsEqual(model.clusterCenters(), expectedCenter);

model = KMeans.train(data.rdd(), 1, 1, 1, KMeans.RANDOM());
assertSetsEqual(model.clusterCenters(), expectedCenter);
}

@Test
Expand All @@ -100,5 +104,12 @@ public void runKMeansUsingConstructor() {
JavaRDD<double[]> data = sc.parallelize(points, 2);
KMeansModel model = new KMeans().setK(1).setMaxIterations(5).run(data.rdd());
assertSetsEqual(model.clusterCenters(), expectedCenter);

model = new KMeans().setK(1)
.setMaxIterations(1)
.setRuns(1)
.setInitializationMode(KMeans.RANDOM())
.run(data.rdd());
assertSetsEqual(model.clusterCenters(), expectedCenter);
}
}

0 comments on commit 6caec3f

Please sign in to comment.