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

    https://github.com/apache/spark/pull/11119#discussion_r83521550
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/ml/clustering/KMeansSuite.scala ---
    @@ -137,18 +143,69 @@ class KMeansSuite extends SparkFunSuite with 
MLlibTestSparkContext with DefaultR
           assert(model.clusterCenters === model2.clusterCenters)
         }
         val kmeans = new KMeans()
    -    testEstimatorAndModelReadWrite(kmeans, dataset, 
KMeansSuite.allParamSettings, checkModelData)
    +    testEstimatorAndModelReadWrite(kmeans, dataset, 
KMeansSuite.allParamSettings, checkModelData,
    +      Map("initialModel" -> (checkModelData _).asInstanceOf[(Any, Any) => 
Unit]))
    +  }
    +
    +  test("Initialize using a trained model") {
    +    val kmeans = new KMeans().setK(k).setSeed(1).setMaxIter(1)
    +    val oneIterModel = kmeans.fit(dataset)
    +    val twoIterModel = kmeans.copy(ParamMap(ParamPair(kmeans.maxIter, 
2))).fit(dataset)
    +    val oneMoreIterModel = 
kmeans.setInitialModel(oneIterModel).fit(dataset)
    +
    +    assert(oneMoreIterModel.getK === k)
    +
    +    twoIterModel.clusterCenters.zip(oneMoreIterModel.clusterCenters)
    +      .foreach { case (center1, center2) => assert(center1 ~== center2 
absTol 1E-8) }
    +  }
    +
    +  test("Initialize using a model with wrong dimension of cluster centers") 
{
    +    val kmeans = new KMeans().setK(k).setSeed(1).setMaxIter(1)
    +
    +    val wrongDimModel = KMeansSuite.generateRandomKMeansModel(4, k)
    +    val wrongDimModelThrown = intercept[IllegalArgumentException] {
    +      kmeans.setInitialModel(wrongDimModel).fit(dataset)
    +    }
    +    assert(wrongDimModelThrown.getMessage.contains("mismatched dimension"))
    +  }
    +
    +  test("Infer K from an initial model") {
    +    val kmeans = new KMeans().setK(5)
    +    val testNewK = 10
    +    val randomModel = KMeansSuite.generateRandomKMeansModel(dim, testNewK)
    +    assert(kmeans.setInitialModel(randomModel).getK === testNewK)
    +
    +    val differentKRandomModel = KMeansSuite.generateRandomKMeansModel(dim, 
testNewK + 1)
    +    assert(kmeans.setInitialModel(differentKRandomModel).getK === testNewK 
+ 1)
    +  }
    +
    +  test("Reset K after setting initial model") {
    --- End diff --
    
    If we do change the behavior of just ignoring `k` as noted above, then this 
test will be invalid. In that case, we should just check that `k` is properly 
ignored, like:
    
    ````scala
      test("Ignore k if initialModel is set") {
        val kmeans = new KMeans()
    
        val m1 = KMeansSuite.generateRandomKMeansModel(dim, k)
        // ignore k if initialModel is set
        assert(kmeans.setInitialModel(m1).setK(k - 1).getK === k)
        kmeans.clear(kmeans.initialModel)
        // k is not ignored after initialModel is cleared
        assert(kmeans.setK(k - 1).getK === k - 1)
    }
    ````


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to