You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by hhbyyh <gi...@git.apache.org> on 2017/02/01 06:53:12 UTC

[GitHub] spark pull request #15415: [SPARK-14503][ML] spark.ml API for FPGrowth

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

    https://github.com/apache/spark/pull/15415#discussion_r98836498
  
    --- Diff: mllib/src/main/scala/org/apache/spark/ml/fpm/FPGrowth.scala ---
    @@ -0,0 +1,251 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.ml.fpm
    +
    +import org.apache.hadoop.fs.Path
    +
    +import org.apache.spark.annotation.{Experimental, Since}
    +import org.apache.spark.ml.{Estimator, Model}
    +import org.apache.spark.ml.param._
    +import org.apache.spark.ml.param.shared.{HasFeaturesCol, HasPredictionCol}
    +import org.apache.spark.ml.util._
    +import org.apache.spark.mllib.fpm.{FPGrowth => MLlibFPGrowth, FPGrowthModel => MLlibFPGrowthModel}
    +import org.apache.spark.sql.{DataFrame, _}
    +import org.apache.spark.sql.functions._
    +import org.apache.spark.sql.types.{ArrayType, StringType, StructType}
    +
    +/**
    + * Common params for FPGrowth and FPGrowthModel
    + */
    +private[fpm] trait FPGrowthParams extends Params with HasFeaturesCol with HasPredictionCol {
    +
    +  /**
    +   * Validates and transforms the input schema.
    +   * @param schema input schema
    +   * @return output schema
    +   */
    +  protected def validateAndTransformSchema(schema: StructType): StructType = {
    +    SchemaUtils.checkColumnType(schema, $(featuresCol), new ArrayType(StringType, false))
    +    SchemaUtils.appendColumn(schema, $(predictionCol), new ArrayType(StringType, false))
    +  }
    +
    +  /**
    +   * Minimal support level of the frequent pattern. [0.0, 1.0]. Any pattern that appears
    +   * more than (minSupport * size-of-the-dataset) times will be output
    +   * Default: 0.3
    +   * @group param
    +   */
    +  @Since("2.2.0")
    +  val minSupport: DoubleParam = new DoubleParam(this, "minSupport",
    +    "the minimal support level of the frequent pattern (Default: 0.3)",
    +    ParamValidators.inRange(0.0, 1.0))
    +  setDefault(minSupport -> 0.3)
    +
    +  /** @group getParam */
    +  @Since("2.2.0")
    +  def getMinSupport: Double = $(minSupport)
    +
    +  /**
    +   * Number of partitions used by parallel FP-growth
    +   * @group param
    +   */
    +  @Since("2.2.0")
    +  val numPartitions: IntParam = new IntParam(this, "numPartitions",
    +    "Number of partitions used by parallel FP-growth", ParamValidators.gtEq[Int](1))
    +
    +  /** @group getParam */
    +  @Since("2.2.0")
    +  def getNumPartitions: Int = $(numPartitions)
    +
    +}
    +
    +/**
    + * :: Experimental ::
    + * A parallel FP-growth algorithm to mine frequent itemsets.
    + *
    + * @see [[http://dx.doi.org/10.1145/1454008.1454027 Li et al., PFP: Parallel FP-Growth for Query
    + *  Recommendation]]
    + */
    +@Since("2.2.0")
    +@Experimental
    +class FPGrowth @Since("2.2.0") (
    +    @Since("2.2.0") override val uid: String)
    +  extends Estimator[FPGrowthModel] with FPGrowthParams with DefaultParamsWritable {
    +
    +  @Since("2.2.0")
    +  def this() = this(Identifiable.randomUID("FPGrowth"))
    +
    +  /** @group setParam */
    +  @Since("2.2.0")
    +  def setMinSupport(value: Double): this.type = set(minSupport, value)
    +
    +  /** @group setParam */
    +  @Since("2.2.0")
    +  def setNumPartitions(value: Int): this.type = set(numPartitions, value)
    +
    +  /** @group setParam */
    +  @Since("2.2.0")
    +  def setFeaturesCol(value: String): this.type = set(featuresCol, value)
    +
    +  /** @group setParam */
    +  @Since("2.2.0")
    +  def setPredictionCol(value: String): this.type = set(predictionCol, value)
    +
    +  def fit(dataset: Dataset[_]): FPGrowthModel = {
    +    val data = dataset.select($(featuresCol)).rdd.map(r => r.getSeq[String](0).toArray)
    +    val parentModel = new MLlibFPGrowth().setMinSupport($(minSupport)).run(data)
    +    copyValues(new FPGrowthModel(uid, parentModel))
    +  }
    +
    +  @Since("2.2.0")
    +  override def transformSchema(schema: StructType): StructType = {
    +    validateAndTransformSchema(schema)
    +  }
    +
    +  override def copy(extra: ParamMap): FPGrowth = defaultCopy(extra)
    +}
    +
    +
    +@Since("2.2.0")
    +object FPGrowth extends DefaultParamsReadable[FPGrowth] {
    +
    +  @Since("2.2.0")
    +  override def load(path: String): FPGrowth = super.load(path)
    +}
    +
    +/**
    + * :: Experimental ::
    + * Model fitted by FPGrowth.
    + *
    + * @param parentModel a model trained by spark.mllib.fpm.FPGrowth
    + */
    +@Since("2.2.0")
    +@Experimental
    +class FPGrowthModel private[ml] (
    +    @Since("2.2.0") override val uid: String,
    +    private val parentModel: MLlibFPGrowthModel[_])
    +  extends Model[FPGrowthModel] with FPGrowthParams with MLWritable {
    +
    +  /**
    +   * minimal confidence for generating Association Rule
    +   * Default: 0.8
    +   * @group param
    +   */
    +  @Since("2.2.0")
    +  val minConfidence: DoubleParam = new DoubleParam(this, "minConfidence",
    +    "minimal confidence for generating Association Rule (Default: 0.8)",
    +    ParamValidators.inRange(0.0, 1.0))
    +  setDefault(minConfidence -> 0.8)
    +
    +  /** @group getParam */
    +  @Since("2.2.0")
    +  def getMinConfidence: Double = $(minConfidence)
    +
    +  /** @group setParam */
    +  @Since("2.2.0")
    +  def setMinConfidence(value: Double): this.type = set(minConfidence, value)
    +
    +  @Since("2.2.0")
    +  override def transform(dataset: Dataset[_]): DataFrame = {
    +    val associationRules = getAssociationRules.rdd.map(r =>
    --- End diff --
    
    Good point. Thanks @aray. I've tried to leverage lazy val to avoid unnecessary computation.


---
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