-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-22884][ML][TESTS] ML test for StructuredStreaming: spark.ml.clustering #20319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
97c96b6
b6e06e8
dc7e708
eee38f2
b2aa3c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,13 +20,14 @@ package org.apache.spark.ml.clustering | |
| import org.apache.spark.{SparkException, SparkFunSuite} | ||
| import org.apache.spark.ml.linalg.{Vector, Vectors} | ||
| import org.apache.spark.ml.param.ParamMap | ||
| import org.apache.spark.ml.util.{DefaultReadWriteTest, MLTestingUtils} | ||
| import org.apache.spark.ml.util.{DefaultReadWriteTest, MLTest, MLTestingUtils} | ||
| import org.apache.spark.mllib.clustering.DistanceMeasure | ||
| import org.apache.spark.mllib.util.MLlibTestSparkContext | ||
| import org.apache.spark.sql.Dataset | ||
|
|
||
| class BisectingKMeansSuite | ||
| extends SparkFunSuite with MLlibTestSparkContext with DefaultReadWriteTest { | ||
| class BisectingKMeansSuite extends MLTest with DefaultReadWriteTest { | ||
|
|
||
| import Encoders._ | ||
|
|
||
| final val k = 5 | ||
| @transient var dataset: Dataset[_] = _ | ||
|
|
@@ -65,10 +66,12 @@ class BisectingKMeansSuite | |
|
|
||
| // Verify fit does not fail on very sparse data | ||
| val model = bkm.fit(sparseDataset) | ||
| val result = model.transform(sparseDataset) | ||
| val numClusters = result.select("prediction").distinct().collect().length | ||
| // Verify we hit the edge case | ||
| assert(numClusters < k && numClusters > 1) | ||
|
|
||
| testTransformerByGlobalCheckFunc[Vector](sparseDataset.toDF(), model, "prediction") { rows => | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| val numClusters = rows.distinct.length | ||
| // Verify we hit the edge case | ||
| assert(numClusters < k && numClusters > 1) | ||
| } | ||
| } | ||
|
|
||
| test("setter/getter") { | ||
|
|
@@ -102,17 +105,14 @@ class BisectingKMeansSuite | |
| val model = bkm.fit(dataset) | ||
| assert(model.clusterCenters.length === k) | ||
|
|
||
| val transformed = model.transform(dataset) | ||
| val expectedColumns = Array("features", predictionColName) | ||
| expectedColumns.foreach { column => | ||
| assert(transformed.columns.contains(column)) | ||
| testTransformerByGlobalCheckFunc[Vector](dataset.toDF(), model, | ||
| "features", predictionColName) { rows => | ||
| val clusters = rows.map(_.getAs[Int](predictionColName)).toSet | ||
| assert(clusters.size === k) | ||
| assert(clusters === Set(0, 1, 2, 3, 4)) | ||
| assert(model.computeCost(dataset) < 0.1) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These checks which do not use "rows" should go outside of testTransformerByGlobalCheckFunc |
||
| assert(model.hasParent) | ||
| } | ||
| val clusters = | ||
| transformed.select(predictionColName).rdd.map(_.getInt(0)).distinct().collect().toSet | ||
| assert(clusters.size === k) | ||
| assert(clusters === Set(0, 1, 2, 3, 4)) | ||
| assert(model.computeCost(dataset) < 0.1) | ||
| assert(model.hasParent) | ||
|
|
||
| // Check validity of model summary | ||
| val numRows = dataset.count() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.ml.clustering | ||
|
|
||
| import org.apache.spark.ml.linalg.Vector | ||
| import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | ||
|
|
||
| private[clustering] object Encoders { | ||
| implicit val vectorEncoder = ExpressionEncoder[Vector]() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a better solution to provide an implicit
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for asking; you shouldn't need to do this. I'll comment on BisectingKMeansSuite.scala |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import testImplicits._instead