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

    https://github.com/apache/spark/pull/15415#discussion_r98959499
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/fpm/AssociationRules.scala ---
    @@ -0,0 +1,234 @@
    +/*
    + * 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.HasPredictionCol
    +import org.apache.spark.ml.util.{DefaultParamsReader, DefaultParamsWriter, 
_}
    +import org.apache.spark.mllib.fpm.{AssociationRules => 
MLlibAssociationRules}
    +import org.apache.spark.mllib.fpm.FPGrowth.FreqItemset
    +import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}
    +import org.apache.spark.sql.functions._
    +import org.apache.spark.sql.types._
    +
    +
    +/**
    + * Common params for AssociationRules and AssociationRulesModel
    + */
    +private[fpm] trait AssociationRulesParam extends Params with 
HasPredictionCol{
    +
    +  /**
    +   * Param for items column name. Items must be array of Strings.
    +   * Default: "items"
    +   *
    +   * @group param
    +   */
    +  final val itemsCol: Param[String] = new Param[String](this, "itemsCol", 
"column name in the" +
    +    " DataFrame containing the items")
    +
    +
    +  /** @group getParam */
    +  @Since("2.2.0")
    +  final def getItemsCol: String = $(itemsCol)
    +  setDefault(itemsCol -> "items")
    +}
    +
    +@Since("2.2.0")
    +object AssociationRules extends DefaultParamsReadable[AssociationRules] {
    +
    +  @Since("2.2.0")
    +  override def load(path: String): AssociationRules = super.load(path)
    +}
    +
    +/**
    + * :: Experimental ::
    + *
    + * Generates association rules from frequent itemsets DataFrame("items", 
"freq"). This method only
    + * generates association rules which have a single item as the consequent.
    + */
    +@Since("2.2.0")
    +@Experimental
    +class AssociationRules(override val uid: String)
    +  extends Estimator[AssociationRulesModel] with AssociationRulesParam with 
HasPredictionCol {
    +
    +  @Since("2.2.0")
    +  def this() = this(Identifiable.randomUID("AssociationRules"))
    +
    +  /** @group setParam */
    +  @Since("2.2.0")
    +  def setItemsCol(value: String): this.type = set(itemsCol, value)
    +
    +  /**
    +   * Param for frequency column name. Data type should be Long.
    +   * Default: "freq"
    +   *
    +   * @group param
    +   */
    +  final val freqCol: Param[String] = new Param[String](this, "freqCol", 
"frequency column name")
    +
    +
    +  /** @group getParam */
    +  @Since("2.2.0")
    +  final def getFreqCol: String = $(freqCol)
    +
    +  /** @group setParam */
    +  @Since("2.2.0")
    +  def setFreqCol(value: String): this.type = set(freqCol, value)
    +  setDefault(freqCol -> "freq")
    +
    +  /**
    +   * Param for minimum confidence, range [0.0, 1.0].
    +    *
    +    * @group param
    +   */
    +  final val minConfidence: DoubleParam = new DoubleParam(this, 
"minConfidence", "min confidence",
    +    ParamValidators.inRange(0.0, 1.0))
    +
    +  /** @group getParam */
    +  @Since("2.2.0")
    +  final def getMinConfidence: Double = $(minConfidence)
    +
    +  /** @group setParam */
    +  @Since("2.2.0")
    +  def setMinConfidence(value: Double): this.type = set(minConfidence, 
value)
    +  setDefault(minConfidence -> 0.8)
    +
    +  /**
    +   * Computes the association rules with confidence above 
[[minConfidence]].
    +   *
    +   * @return a DataFrame("antecedent", "consequent", "confidence") 
containing the association
    +   *         rules.
    +   */
    +  @Since("2.2.0")
    +  override def fit(dataset: Dataset[_]): AssociationRulesModel = {
    +    val freqItemSetRdd = dataset.select($(itemsCol), $(freqCol)).rdd
    +      .map(row => new FreqItemset(row.getSeq[String](0).toArray, 
row.getLong(1)))
    +
    +    val spark = SparkSession.builder().getOrCreate()
    +    import spark.implicits._
    +    val oldModel = new MLlibAssociationRules()
    +      .setMinConfidence($(minConfidence))
    +      .run(freqItemSetRdd)
    +      .map(r => (r.antecedent, r.consequent, r.confidence))
    +      .toDF("antecedent", "consequent", "confidence")
    +    copyValues(new AssociationRulesModel(uid, oldModel).setParent(this))
    +  }
    +
    +  @Since("2.2.0")
    +  override def transformSchema(schema: StructType): StructType = {
    +    SchemaUtils.checkColumnType(schema, $(itemsCol), new 
ArrayType(StringType, false))
    +    SchemaUtils.checkColumnType(schema, $(freqCol), LongType)
    +    SchemaUtils.appendColumn(schema, $(predictionCol), new 
ArrayType(StringType, false))
    +  }
    +
    +  override def copy(extra: ParamMap): AssociationRules = defaultCopy(extra)
    +
    +}
    +
    +
    +/**
    + * :: Experimental ::
    + * Model fitted by AssociationRules.
    + *
    + * @param associationRules AssociationRules
    + */
    +@Since("2.2.0")
    +@Experimental
    +class AssociationRulesModel private[ml] (
    +    @Since("2.2.0") override val uid: String,
    +    val associationRules: DataFrame)
    --- End diff --
    
    need Since annotation


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

Reply via email to