Github user sethah commented on a diff in the pull request:

    https://github.com/apache/spark/pull/15450#discussion_r84923158
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/mllib/clustering/KMeansSuite.scala ---
    @@ -64,30 +66,55 @@ class KMeansSuite extends SparkFunSuite with 
MLlibTestSparkContext {
         assert(model.clusterCenters.head ~== center absTol 1E-5)
       }
     
    -  test("no distinct points") {
    +  test("fewer distinct points than clusters") {
         val data = sc.parallelize(
           Array(
             Vectors.dense(1.0, 2.0, 3.0),
             Vectors.dense(1.0, 2.0, 3.0),
             Vectors.dense(1.0, 2.0, 3.0)),
           2)
    -    val center = Vectors.dense(1.0, 2.0, 3.0)
     
    -    // Make sure code runs.
    -    var model = KMeans.train(data, k = 2, maxIterations = 1)
    -    assert(model.clusterCenters.size === 2)
    -  }
    +    var model = KMeans.train(data, k = 2, maxIterations = 1, 
initializationMode = "random")
    +    assert(model.clusterCenters.length === 1)
     
    -  test("more clusters than points") {
    -    val data = sc.parallelize(
    -      Array(
    -        Vectors.dense(1.0, 2.0, 3.0),
    -        Vectors.dense(1.0, 3.0, 4.0)),
    -      2)
    +    model = KMeans.train(data, k = 2, maxIterations = 1, 
initializationMode = "k-means||")
    +    assert(model.clusterCenters.length === 1)
    +  }
     
    -    // Make sure code runs.
    -    var model = KMeans.train(data, k = 3, maxIterations = 1)
    -    assert(model.clusterCenters.size === 3)
    +    test("unique cluster centers") {
    +    val rng = new Random(seed)
    +    val numDistinctPoints = 10
    +    val points = (0 until numDistinctPoints).map(i => 
Vectors.dense(Array.fill(3)(rng.nextDouble)))
    +    val data = sc.parallelize(points.flatMap(Array.fill(1 + 
rng.nextInt(3))(_)), 2)
    +    val normedData = data.map(new VectorWithNorm(_))
    +
    +    // less centers than k
    +    val km = new KMeans().setK(50)
    +      .setMaxIterations(5)
    +      .setInitializationMode("k-means||")
    +      .setInitializationSteps(10)
    +      .setSeed(seed)
    +    val initialCenters = km.initKMeansParallel(normedData).map(_.vector)
    +    assert(initialCenters.length === initialCenters.distinct.length)
    +    assert(initialCenters.length <= numDistinctPoints)
    +
    +    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(5)
    +      .setInitializationMode("k-means||")
    +      .setInitializationSteps(10)
    +      .setSeed(seed)
    +    val initialCenters2 = km2.initKMeansParallel(normedData).map(_.vector)
    +    assert(initialCenters2.length === initialCenters2.distinct.length)
    +    assert(initialCenters2.length === 10)
    --- End diff --
    
    minor/nit: maybe make `k` a val here and use that instead. Since we use 10 
for something else above, this can be obfuscated in the future.


---
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]

Reply via email to