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

    https://github.com/apache/spark/pull/11119#discussion_r78690235
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/ml/clustering/KMeansSuite.scala ---
    @@ -139,16 +146,72 @@ class KMeansSuite extends SparkFunSuite with 
MLlibTestSparkContext with DefaultR
         val kmeans = new KMeans()
         testEstimatorAndModelReadWrite(kmeans, dataset, 
KMeansSuite.allParamSettings, checkModelData)
       }
    +
    +  test("Initialize using given cluster centers") {
    +    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)
    +
    +    twoIterModel.clusterCenters.zip(oneMoreIterModel.clusterCenters)
    +      .foreach { case (center1, center2) => assert(center1 ~== center2 
absTol 1E-8) }
    +  }
    +
    +  test("Initialize using wrong model") {
    +    val kmeans = new KMeans().setK(k).setSeed(1).setMaxIter(10)
    +    val wrongTypeModel = new KMeansSuite.MockModel()
    +
    +    withClue("The type of an initial model should only be a KMeansModel.") 
{
    +      intercept[IllegalArgumentException] {
    +        kmeans.setInitialModel(wrongTypeModel).fit(dataset)
    +      }
    +    }
    +
    +    val wrongKModel = KMeansSuite.generateRandomKMeansModel(3, k + 1)
    +    withClue("The number of clusters set in the given model should be the 
same with the one set" +
    +      " in the KMeans estimator.") {
    +      intercept[IllegalArgumentException] {
    +        kmeans.setInitialModel(wrongKModel).fit(dataset)
    +      }
    +    }
    +
    +    val wrongDimModel = KMeansSuite.generateRandomKMeansModel(4, k)
    +    withClue("The dimension of points in the model should be the same with 
the dimension of the" +
    --- End diff --
    
    `withClue` just prepends the provided message to the error message when the 
exception is not thrown as expected. I think we should check that the correct 
exception is thrown:
    
    ````scala
    val thrown = intercept[IllegalArgumentException] {
      ...
    }
    assert(thrown.getMessage.contains("mismatched dimension"))
    ````


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