Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions mllib/src/main/scala/org/apache/spark/ml/clustering/KMeans.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,6 @@ class KMeansModel private[ml] (
@Since("2.0.0")
def clusterCenters: Array[Vector] = parentModel.clusterCenters.map(_.asML)

/**
* Return the K-means cost (sum of squared distances of points to their nearest center) for this
* model on the given data.
*
* @deprecated This method is deprecated and will be removed in 3.0.0. Use ClusteringEvaluator
* instead. You can also get the cost on the training dataset in the summary.
*/
@deprecated("This method is deprecated and will be removed in 3.0.0. Use ClusteringEvaluator " +
"instead. You can also get the cost on the training dataset in the summary.", "2.4.0")
@Since("2.0.0")
def computeCost(dataset: Dataset[_]): Double = {
SchemaUtils.validateVectorCompatibleColumn(dataset.schema, getFeaturesCol)
val data = DatasetUtils.columnToOldVector(dataset, getFeaturesCol)
parentModel.computeCost(data)
}

/**
* Returns a [[org.apache.spark.ml.util.GeneralMLWriter]] instance for this ML instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class KMeansSuite extends MLTest with DefaultReadWriteTest with PMMLReadWriteTes
assert(clusters === Set(0, 1, 2, 3, 4))
}

assert(model.computeCost(dataset) < 0.1)
assert(model.hasParent)

// Check validity of model summary
Expand All @@ -132,7 +131,6 @@ class KMeansSuite extends MLTest with DefaultReadWriteTest with PMMLReadWriteTes
}
assert(summary.cluster.columns === Array(predictionColName))
assert(summary.trainingCost < 0.1)
assert(model.computeCost(dataset) == summary.trainingCost)
val clusterSizes = summary.clusterSizes
assert(clusterSizes.length === k)
assert(clusterSizes.sum === numRows)
Expand Down Expand Up @@ -201,15 +199,15 @@ class KMeansSuite extends MLTest with DefaultReadWriteTest with PMMLReadWriteTes
}

test("KMean with Array input") {
def trainAndComputeCost(dataset: Dataset[_]): Double = {
def trainAndGetCost(dataset: Dataset[_]): Double = {
val model = new KMeans().setK(k).setMaxIter(1).setSeed(1).fit(dataset)
model.computeCost(dataset)
model.summary.trainingCost
}

val (newDataset, newDatasetD, newDatasetF) = MLTestingUtils.generateArrayFeatureDataset(dataset)
val trueCost = trainAndComputeCost(newDataset)
val doubleArrayCost = trainAndComputeCost(newDatasetD)
val floatArrayCost = trainAndComputeCost(newDatasetF)
val trueCost = trainAndGetCost(newDataset)
val doubleArrayCost = trainAndGetCost(newDatasetD)
val floatArrayCost = trainAndGetCost(newDatasetF)

// checking the cost is fine enough as a sanity check
assert(trueCost ~== doubleArrayCost absTol 1e-6)
Expand Down
3 changes: 3 additions & 0 deletions project/MimaExcludes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ object MimaExcludes {

// Exclude rules for 3.0.x
lazy val v30excludes = v24excludes ++ Seq(
// [SPARK-25867] Remove KMeans computeCost
ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.spark.ml.clustering.KMeansModel.computeCost"),

// [SPARK-26090] Resolve most miscellaneous deprecation and build warnings for Spark 3
ProblemFilters.exclude[MissingClassProblem]("org.apache.spark.mllib.stat.test.BinarySampleBeanInfo"),
ProblemFilters.exclude[MissingClassProblem]("org.apache.spark.mllib.regression.LabeledPointBeanInfo"),
Expand Down
16 changes: 0 additions & 16 deletions python/pyspark/ml/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,20 +335,6 @@ def clusterCenters(self):
"""Get the cluster centers, represented as a list of NumPy arrays."""
return [c.toArray() for c in self._call_java("clusterCenters")]

@since("2.0.0")
def computeCost(self, dataset):
"""
Return the K-means cost (sum of squared distances of points to their nearest center)
for this model on the given data.

..note:: Deprecated in 2.4.0. It will be removed in 3.0.0. Use ClusteringEvaluator instead.
You can also get the cost on the training dataset in the summary.
"""
warnings.warn("Deprecated in 2.4.0. It will be removed in 3.0.0. Use ClusteringEvaluator "
"instead. You can also get the cost on the training dataset in the summary.",
DeprecationWarning)
return self._call_java("computeCost", dataset)

@property
@since("2.1.0")
def hasSummary(self):
Expand Down Expand Up @@ -387,8 +373,6 @@ class KMeans(JavaEstimator, HasDistanceMeasure, HasFeaturesCol, HasPredictionCol
>>> centers = model.clusterCenters()
>>> len(centers)
2
>>> model.computeCost(df)
2.0
>>> transformed = model.transform(df).select("features", "prediction")
>>> rows = transformed.collect()
>>> rows[0].prediction == rows[1].prediction
Expand Down