Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/5450#discussion_r28216481
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/clustering/PowerIterationClustering.scala
---
@@ -38,7 +43,63 @@ import org.apache.spark.util.random.XORShiftRandom
@Experimental
class PowerIterationClusteringModel(
val k: Int,
- val assignments: RDD[PowerIterationClustering.Assignment]) extends
Serializable
+ val assignments: RDD[PowerIterationClustering.Assignment]) extends
Saveable with Serializable {
+
+ override def save(sc: SparkContext, path: String): Unit = {
+ PowerIterationClusteringModel.SaveLoadV1_0.save(sc, this, path)
+ }
+
+ override protected def formatVersion: String = "1.0"
+}
+
+object PowerIterationClusteringModel extends
Loader[PowerIterationClusteringModel] {
+ override def load(sc: SparkContext, path: String):
PowerIterationClusteringModel = {
+ PowerIterationClusteringModel.SaveLoadV1_0.load(sc, path)
+ }
+
+ private[clustering]
+ object SaveLoadV1_0 {
+
+ private val thisFormatVersion = "1.0"
+
+ private[clustering]
+ val thisClassName =
"org.apache.spark.mllib.clustering.PowerIterationClusteringModel"
+
+ def save(sc: SparkContext, model: PowerIterationClusteringModel, path:
String): Unit = {
+ val sqlContext = new SQLContext(sc)
+ import sqlContext.implicits._
+
+ val metadata = compact(render(
+ ("class" -> thisClassName) ~ ("version" -> thisFormatVersion) ~
("k" -> model.k)))
+ sc.parallelize(Seq(metadata),
1).saveAsTextFile(Loader.metadataPath(path))
+
+ val dataRDD = model.assignments.map(x => (x.id, x.cluster)).toDF()
+ dataRDD.saveAsParquetFile(Loader.dataPath(path))
+ }
+
+ def load(sc: SparkContext, path: String):
PowerIterationClusteringModel = {
+ implicit val formats = DefaultFormats
+ val sqlContext = new SQLContext(sc)
+
+ val (className, formatVersion, metadata) = Loader.loadMetadata(sc,
path)
+ assert(className == thisClassName)
+ assert(formatVersion == thisFormatVersion)
+
+ val k = (metadata \ "k").extract[Int]
+ val assignments = sqlContext.parquetFile(Loader.dataPath(path))
+ Loader.checkSchema[(Long, Int)](assignments.schema)
+
+ val assignmentsRDD = assignments.map {
+ case Row(id: Long, cluster: Int) => new
PowerIterationClustering.Assignment(id, cluster)
+ }
+
+ val realK = assignmentsRDD.map(_.cluster).distinct().collect().size
--- End diff --
This is expensive. Maybe we can ignore this check (or put it in the tests).
---
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]