Github user sethah commented on a diff in the pull request:
https://github.com/apache/spark/pull/11119#discussion_r77060522
--- Diff: mllib/src/main/scala/org/apache/spark/ml/clustering/KMeans.scala
---
@@ -333,13 +384,44 @@ class KMeans @Since("1.5.0") (
override def transformSchema(schema: StructType): StructType = {
validateAndTransformSchema(schema)
}
+
+ @Since("2.0.0")
+ override def write: MLWriter = new KMeans.KMeansWriter(this)
}
@Since("1.6.0")
object KMeans extends DefaultParamsReadable[KMeans] {
@Since("1.6.0")
override def load(path: String): KMeans = super.load(path)
+
+ @Since("2.0.0")
+ override def read: MLReader[KMeans] = new KMeansReader
+
+ /** [[MLWriter]] instance for [[KMeans]] */
+ private[KMeans] class KMeansWriter(instance: KMeans) extends MLWriter {
+ import org.json4s.JsonDSL._
+
+ override protected def saveImpl(path: String): Unit = {
+ DefaultParamsWriter.saveInitialModel(instance, path)
+ DefaultParamsWriter.saveMetadata(instance, path, sc)
+ }
+ }
+
+ private class KMeansReader extends MLReader[KMeans] {
+
+ /** Checked against metadata when loading model */
+ private val className = classOf[KMeans].getName
+
+ override def load(path: String): KMeans = {
+ val metadata = DefaultParamsReader.loadMetadata(path, sc, className)
+ val instance = new KMeans(metadata.uid)
+
+ DefaultParamsReader.getAndSetParams(instance, metadata)
+ DefaultParamsReader.loadInitialModel[KMeansModel](instance, path, sc)
--- End diff --
We should pass metadata to this method. There is no reason to read it twice.
---
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]