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

    https://github.com/apache/spark/pull/7258#discussion_r34183085
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala 
---
    @@ -0,0 +1,209 @@
    +/*
    + * 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.mllib.fpm
    +
    +import org.apache.spark.annotation.Experimental
    +import org.apache.spark.rdd.RDD
    +
    +/**
    + *
    + * :: Experimental ::
    + *
    + * A parallel PrefixSpan algorithm to mine sequential pattern.
    + * The PrefixSpan algorithm is described in
    + * [[http://doi.org/10.1109/ICDE.2001.914830]].
    + *
    + * @param minSupport the minimal support level of the sequential pattern, 
any pattern appears
    + *                   more than  (minSupport * size-of-the-dataset) times 
will be output
    + * @param maxPatternLength the maximal length of the sequential pattern, 
any pattern appears
    + *                   less than maxPatternLength will be output
    + *
    + * @see [[https://en.wikipedia.org/wiki/Sequential_Pattern_Mining 
Sequential Pattern Mining
    + *       (Wikipedia)]]
    + */
    +@Experimental
    +class PrefixSpan(
    +    private var minSupport: Double,
    +    private var maxPatternLength: Int) extends java.io.Serializable {
    +
    +    private var absMinSupport: Int = 0
    +
    +  /**
    +   * Constructs a default instance with default parameters
    +   * {minSupport: `0.1`, maxPatternLength: 10}.
    +   */
    +  def this() = this(0.1, 10)
    +
    +  /**
    +   * Sets the minimal support level (default: `0.1`).
    +   */
    +  def setMinSupport(minSupport: Double): this.type = {
    +    this.minSupport = minSupport
    +    this
    +  }
    +
    +  /**
    +   * Sets maximal pattern length.
    +   */
    +  def setMaxPatternLength(maxPatternLength: Int): this.type = {
    +    this.maxPatternLength = maxPatternLength
    +    this
    +  }
    +
    +  /**
    +   * Calculate sequential patterns:
    +   * a) find and collect length-one patterns
    +   * b) for each length-one patterns and each sequence,
    +   *    emit (pattern (prefix), suffix sequence) as key-value pairs
    +   * c) group by key and then map value iterator to array
    +   * d) local PrefixSpan on each prefix
    +   * @return sequential patterns
    +   */
    +  def run(sequences: RDD[Array[Int]]): RDD[(Seq[Int], Int)] = {
    +    absMinSupport = getAbsoluteMinSupport(sequences)
    +    val (lengthOnePatternsAndCounts, prefixAndCandidates) =
    +      findLengthOnePatterns(sequences)
    +    val repartitionedRdd = 
makePrefixProjectedDatabases(prefixAndCandidates)
    +    val nextPatterns = getPatternsInLocal(repartitionedRdd)
    +    val allPatterns = lengthOnePatternsAndCounts.map(x => (Seq(x._1), 
x._2)) ++ nextPatterns
    +    allPatterns
    +  }
    +
    +  private def getAbsoluteMinSupport(sequences: RDD[Array[Int]]): Int = {
    +    val result = if (minSupport <= 0) {
    +      0
    +    } else {
    +      val count = sequences.count()
    +      val support = if (minSupport <= 1) minSupport else 1
    +      (support * count).toInt
    +    }
    +    result
    +  }
    +
    +  /**
    +   * Find the patterns that it's length is one
    +   * @param sequences original sequences data
    +   * @return length-one patterns and projection table
    +   */
    +  private def findLengthOnePatterns(
    +      sequences: RDD[Array[Int]]): (RDD[(Int, Int)], RDD[(Seq[Int], 
Array[Int])]) = {
    +    val LengthOnePatternAndCounts = sequences
    +      .flatMap(_.distinct.map((_, 1)))
    +      .reduceByKey(_ + _)
    +    val infrequentLengthOnePatterns: Array[Int] = LengthOnePatternAndCounts
    +      .filter(_._2 < absMinSupport)
    +      .map(_._1)
    +      .collect()
    +    val frequentLengthOnePatterns = LengthOnePatternAndCounts
    --- End diff --
    
    `frequentLengthOnePatterns` -> `frequentLengthOnePatternAndCounts`


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

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

Reply via email to