huaxingao commented on a change in pull request #26739:
[SPARK-29967][ML][PYTHON] KMeans support instance weighting
URL: https://github.com/apache/spark/pull/26739#discussion_r353561521
##########
File path: mllib/src/main/scala/org/apache/spark/ml/clustering/KMeans.scala
##########
@@ -313,12 +314,32 @@ class KMeans @Since("1.5.0") (
@Since("1.5.0")
def setSeed(value: Long): this.type = set(seed, value)
+ /**
+ * Sets the value of param [[weightCol]].
+ * If this is not set or empty, we treat all instance weights as 1.0.
+ * Default is not set, so all instances have weight one.
+ *
+ * @group setParam
+ */
+ @Since("3.0.0")
+ def setWeightCol(value: String): this.type = set(weightCol, value)
+
@Since("2.0.0")
override def fit(dataset: Dataset[_]): KMeansModel = instrumented { instr =>
transformSchema(dataset.schema, logging = true)
val handlePersistence = dataset.storageLevel == StorageLevel.NONE
- val instances = DatasetUtils.columnToOldVector(dataset, getFeaturesCol)
+ val w = if (isDefined(weightCol) && $(weightCol).nonEmpty) {
+ col($(weightCol)).cast(DoubleType)
+ } else {
+ lit(1.0)
+ }
+
+ val instances: RDD[(OldVector, Double)] = dataset.select(
+ DatasetUtils.columnToVector(dataset, getFeaturesCol),
Review comment:
I will update the format.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]