Github user jkbradley commented on a diff in the pull request:
https://github.com/apache/spark/pull/9267#discussion_r48585210
--- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/FPGrowth.scala ---
@@ -53,6 +65,96 @@ class FPGrowthModel[Item: ClassTag] @Since("1.3.0") (
val associationRules = new AssociationRules(confidence)
associationRules.run(freqItemsets)
}
+
+ override def save(sc: SparkContext, path: String): Unit = {
+ FPGrowthModel.SaveLoadV1_0.save(this, path)
+ }
+
+ override protected val formatVersion: String = "1.0"
+}
+
+object FPGrowthModel extends Loader[FPGrowthModel[_]] {
+
+ override def load(sc: SparkContext, path: String): FPGrowthModel[_] = {
+ val inferredItemset = FPGrowthModel.SaveLoadV1_0.inferItemType(sc,
path)
+ FPGrowthModel.SaveLoadV1_0.load(sc, path, inferredItemset)
+ }
+
+ private[fpm] object SaveLoadV1_0 {
+
+ private val thisFormatVersion = "1.0"
+
+ private[fpm] val thisClassName =
"org.apache.spark.mllib.fpm.FPGrowthModel"
+
+ def save[Item: ClassTag: TypeTag](model: FPGrowthModel[Item], path:
String): Unit = {
+ val sc = model.freqItemsets.sparkContext
+ val sqlContext = new SQLContext(sc)
+
+ val metadata = compact(render(
+ ("class" -> thisClassName) ~ ("version" -> thisFormatVersion)))
+ sc.parallelize(Seq(metadata),
1).saveAsTextFile(Loader.metadataPath(path))
+
+ val itemType = ScalaReflection.schemaFor[Item].dataType
+ val fields = Array(StructField("items", ArrayType(itemType)),
+ StructField("freq", LongType))
+ val schema = StructType(fields)
+ val rowDataRDD = model.freqItemsets.map { x =>
+ Row(x.items, x.freq)
+ }
+ sqlContext.createDataFrame(rowDataRDD,
schema).write.parquet(Loader.dataPath(path))
+ }
+
+ def inferItemType(sc: SparkContext, path: String): FreqItemset[_] = {
+ val sqlContext = new SQLContext(sc)
+ val freqItemsets = sqlContext.read.parquet(Loader.dataPath(path))
+ val itemsetType = freqItemsets.schema(0).dataType
--- End diff --
Use the field name instead of an index to get the element in the schema (to
be more robust).
---
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]