Github user srowen commented on a diff in the pull request:
https://github.com/apache/spark/pull/15450#discussion_r84899743
--- Diff:
mllib/src/test/scala/org/apache/spark/mllib/clustering/KMeansSuite.scala ---
@@ -85,9 +101,50 @@ class KMeansSuite extends SparkFunSuite with
MLlibTestSparkContext {
Vectors.dense(1.0, 3.0, 4.0)),
2)
- // Make sure code runs.
- var model = KMeans.train(data, k = 3, maxIterations = 1)
- assert(model.clusterCenters.size === 3)
+ var model = KMeans.train(data, k = 3, maxIterations = 1,
initializationMode = "random")
+ assert(model.clusterCenters.length === 2)
+
+ model = KMeans.train(data, k = 3, maxIterations = 1,
initializationMode = "k-means||")
+ assert(model.clusterCenters.length === 2)
+ }
+
+ test("unique cluster centers") {
+ val rng = new scala.util.Random(42)
+ val points = (0 until 10).map(i =>
Vectors.dense(Array.fill(3)(rng.nextDouble)))
+ val data = sc.parallelize(
+ points.flatMap { point =>
+ Array.fill(rng.nextInt(4))(point)
+ }, 2
+ )
+ val norms = data.map(Vectors.norm(_, 2.0))
+ val zippedData = data.zip(norms).map { case (v, norm) =>
+ new VectorWithNorm(v, norm)
+ }
+ // less centers than k
+ val km = new KMeans().setK(50)
+ .setMaxIterations(10)
+ .setInitializationMode("k-means||")
+ .setInitializationSteps(10)
+ .setSeed(42)
+ val initialCenters = km.initKMeansParallel(zippedData).map(_.vector)
+ assert(initialCenters.length === initialCenters.distinct.length)
+
+ val model = km.run(data)
+ val finalCenters = model.clusterCenters
+ assert(finalCenters.length === finalCenters.distinct.length)
+
+ // run local k-means
+ val km2 = new KMeans().setK(10)
+ .setMaxIterations(10)
+ .setInitializationMode("k-means||")
+ .setInitializationSteps(10)
+ .setSeed(42)
+ val initialCenters2 = km2.initKMeansParallel(zippedData).map(_.vector)
+ assert(initialCenters2.length === initialCenters2.distinct.length)
--- End diff --
This condition failed, though it should be OK. The problem was that the
data setup maps each of 10 distinct points to 0-3 copies, meaning that there
may be less than 10 distinct points in the end. I just make that 1-3 copies and
it works.
Fixed the doc problem too, thanks.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]