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

    https://github.com/apache/spark/pull/7258#discussion_r34269688
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala 
---
    @@ -0,0 +1,252 @@
    +/*
    + * 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.Logging
    +import org.apache.spark.annotation.Experimental
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.storage.StorageLevel
    +
    +/**
    + *
    + * :: 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 (
    +    private var minSupport: Double,
    +    private var maxPatternLength: Int) extends Logging with Serializable {
    +
    +  /**
    +   * 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 = {
    +    require(minSupport >= 0 && minSupport <= 1)
    +    this.minSupport = minSupport
    +    this
    +  }
    +
    +  /**
    +   * Sets maximal pattern length (default: `10`).
    +   */
    +  def setMaxPatternLength(maxPatternLength: Int): this.type = {
    +    require(maxPatternLength >= 1)
    +    this.maxPatternLength = maxPatternLength
    +    this
    +  }
    +
    +  /**
    +   * Find the complete set of sequential patterns in the input sequences.
    +   * @param sequences input data set, contains a set of sequences,
    +   *                  a sequence is an ordered list of elements.
    +   * @return a set of sequential pattern pairs,
    +   *         the key of pair is pattern (a list of elements),
    +   *         the value of pair is the pattern's support value.
    +   */
    +  def run(sequences: RDD[Array[Int]]): RDD[(Array[Int], Long)] = {
    +    if (sequences.getStorageLevel == StorageLevel.NONE) {
    +      logWarning("Input data is not cached.")
    +    }
    +    val minCount = getAbsoluteMinSupport(sequences)
    +    val (lengthOnePatternsAndCounts, prefixAndCandidates) =
    +      findLengthOnePatterns(minCount, sequences)
    +    val repartitionedRdd = 
makePrefixProjectedDatabases(prefixAndCandidates)
    +    val nextPatterns = getPatternsInLocal(minCount, repartitionedRdd)
    +    val allPatterns = lengthOnePatternsAndCounts.map(x => (Array(x._1), 
x._2)) ++ nextPatterns
    +    allPatterns
    +  }
    +
    +  /**
    +   * Get the absolute minimum support value (sequences count * minSupport).
    +   * @param sequences input data set, contains a set of sequences,
    +   * @return absolute minimum support value,
    +   */
    +  private def getAbsoluteMinSupport(sequences: RDD[Array[Int]]): Long = {
    +    if (minSupport == 0) 0L else (sequences.count() * minSupport).toLong
    +  }
    +
    +  /**
    +   * Generates frequent items by filtering the input data using minimal 
support level.
    +   * @param minCount the absolute minimum support
    +   * @param sequences original sequences data
    +   * @return array of frequent pattern ordered by their frequencies
    +   */
    +  private def getFreqItemAndCounts(
    +      minCount: Long,
    +      sequences: RDD[Array[Int]]): RDD[(Int, Long)] = {
    +    sequences.flatMap(_.distinct.map((_, 1L)))
    +      .reduceByKey(_ + _)
    +      .filter(_._2 >= minCount)
    +  }
    +
    +  /**
    +   * Generates frequent items by filtering the input data using minimal 
support level.
    +   * @param minCount the absolute minimum support
    +   * @param sequences sequences data
    +   * @return array of frequent pattern ordered by their frequencies
    +   */
    +  private def getFreqItemAndCounts(
    +      minCount: Long,
    +      sequences: Array[Array[Int]]): Array[(Int, Long)] = {
    +    sequences.flatMap(_.distinct)
    +      .groupBy(x => x)
    +      .mapValues(_.length.toLong)
    +      .filter(_._2 >= minCount)
    +      .toArray
    +  }
    +
    +  /**
    +   * Get the frequent prefixes' projected database.
    +   * @param frequentPrefixes frequent prefixes
    +   * @param sequences sequences data
    +   * @return prefixes and projected database
    +   */
    +  private def getPatternAndProjectedDatabase(
    +      frequentPrefixes: Array[Int],
    --- End diff --
    
    use a `Set` 
(https://github.com/apache/spark/blob/master/core/src/main/scala/org/apache/spark/util/collection/OpenHashSet.scala)


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