You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by jkbradley <gi...@git.apache.org> on 2016/01/04 23:00:08 UTC

[GitHub] spark pull request: [SPARK-6724] [MLlib] Support model save/load f...

Github user jkbradley commented on a diff in the pull request:

    https://github.com/apache/spark/pull/9267#discussion_r48787485
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/FPGrowth.scala ---
    @@ -49,6 +60,119 @@ class FPGrowthModel[Item: ClassTag] @Since("1.3.0") (
         val associationRules = new AssociationRules(confidence)
         associationRules.run(freqItemsets)
       }
    +
    +  /**
    +   * Save this model to the given path.
    +   * It only works for Item datatypes supported by DataFrames.
    +   *
    +   * This saves:
    +   *  - human-readable (JSON) model metadata to path/metadata/
    +   *  - Parquet formatted data to path/data/
    +   *
    +   * The model may be loaded using [[FPGrowthModel.load]].
    +   *
    +   * @param sc  Spark context used to save model data.
    +   * @param path  Path specifying the directory in which to save this model.
    +   *              If the directory already exists, this method throws an exception.
    +   */
    +  @Since("2.0.0")
    +  override def save(sc: SparkContext, path: String): Unit = {
    +    FPGrowthModel.SaveLoadV1_0.save(this, path)
    +  }
    +
    +  override protected val formatVersion: String = "1.0"
    +}
    +
    +@Since("2.0.0")
    +object FPGrowthModel extends Loader[FPGrowthModel[_]] {
    +
    +  @Since("2.0.0")
    +  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 val thisClassName = "org.apache.spark.mllib.fpm.FPGrowthModel"
    +
    +    def save[Item: ClassTag](model: FPGrowthModel[Item], path: String): Unit = {
    +      val sc = model.freqItemsets.sparkContext
    +      val sqlContext = SQLContext.getOrCreate(sc)
    +
    +      val metadata = compact(render(
    +        ("class" -> thisClassName) ~ ("version" -> thisFormatVersion)))
    +      sc.parallelize(Seq(metadata), 1).saveAsTextFile(Loader.metadataPath(path))
    +
    +      // Get the type of item class
    +      val sample = model.freqItemsets.take(1)(0).items(0)
    --- End diff --
    
    ```take(1)(0)``` -> ```first()```


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org