spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From feynmanliang <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-8998][MLlib] Collect enough frequent pr...
Date Wed, 15 Jul 2015 22:21:55 GMT
Github user feynmanliang commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7412#discussion_r34736797
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -82,20 +84,70 @@ class PrefixSpan private (
           logWarning("Input data is not cached.")
         }
         val minCount = getMinCount(sequences)
    -    val lengthOnePatternsAndCounts =
    -      getFreqItemAndCounts(minCount, sequences).collect()
    -    val prefixAndProjectedDatabase = getPrefixAndProjectedDatabase(
    -      lengthOnePatternsAndCounts.map(_._1), sequences)
    -    val groupedProjectedDatabase = prefixAndProjectedDatabase
    -      .map(x => (x._1.toSeq, x._2))
    -      .groupByKey()
    -      .map(x => (x._1.toArray, x._2.toArray))
    -    val nextPatterns = getPatternsInLocal(minCount, groupedProjectedDatabase)
    -    val lengthOnePatternsAndCountsRdd =
    -      sequences.sparkContext.parallelize(
    -        lengthOnePatternsAndCounts.map(x => (Array(x._1), x._2)))
    -    val allPatterns = lengthOnePatternsAndCountsRdd ++ nextPatterns
    -    allPatterns
    +    val lengthOnePatternsAndCounts = getFreqItemAndCounts(minCount, sequences)
    +    val prefixSuffixPairs = getPrefixSuffixPairs(
    +      lengthOnePatternsAndCounts.map(_._1).collect(), sequences)
    +    var patternsCount: Long = lengthOnePatternsAndCounts.count()
    +    var allPatternAndCounts = lengthOnePatternsAndCounts.map(x => (Array(x._1), x._2))
    +    var currentPrefixSuffixPairs = prefixSuffixPairs
    +    while (patternsCount <= minPatternsBeforeShuffle && currentPrefixSuffixPairs.count()
!= 0) {
    +      val (nextPatternAndCounts, nextPrefixSuffixPairs) =
    +        getPatternCountsAndPrefixSuffixPairs(minCount, currentPrefixSuffixPairs)
    +      patternsCount = nextPatternAndCounts.count().toInt
    +      currentPrefixSuffixPairs = nextPrefixSuffixPairs
    +      allPatternAndCounts = allPatternAndCounts ++ nextPatternAndCounts
    +    }
    +    if (patternsCount > 0) {
    +      val projectedDatabase = currentPrefixSuffixPairs
    +        .map(x => (x._1.toSeq, x._2))
    +        .groupByKey()
    +        .map(x => (x._1.toArray, x._2.toArray))
    +      val nextPatternAndCounts = getPatternsInLocal(minCount, projectedDatabase)
    +      allPatternAndCounts = allPatternAndCounts ++ nextPatternAndCounts
    +    }
    +    allPatternAndCounts
    +  }
    +
    +  /**
    +   * Get the pattern and counts, and prefix suffix pairs
    +   * @param minCount minimum count
    +   * @param prefixSuffixPairs prefix and suffix pairs,
    +   * @return pattern and counts, and prefix suffix pairs
    +   *         (Array[pattern, count], RDD[prefix, suffix ])
    +   */
    +  private def getPatternCountsAndPrefixSuffixPairs(
    +      minCount: Long,
    +      prefixSuffixPairs: RDD[(Array[Int], Array[Int])]):
    +  (RDD[(Array[Int], Long)], RDD[(Array[Int], Array[Int])]) = {
    +    val prefixAndFreqentItemAndCounts = prefixSuffixPairs
    +      .flatMap { case (prefix, suffix) =>
    +      suffix.distinct.map(y => ((prefix.toSeq, y), 1L))
    +    }.reduceByKey(_ + _)
    +      .filter(_._2 >= minCount)
    +    val patternAndCounts = prefixAndFreqentItemAndCounts
    +      .map{ case ((prefix, item), count) => (prefix.toArray :+ item, count) }
    +    val prefixlength = prefixSuffixPairs.first()._1.length
    +    if (prefixlength + 1 >= maxPatternLength) {
    +      (patternAndCounts, prefixSuffixPairs.filter(x => false))
    +    } else {
    +      val frequentItemsMap = prefixAndFreqentItemAndCounts
    +        .keys
    +        .groupByKey()
    +        .mapValues(_.toSet)
    +        .collect
    +        .toMap
    +      val nextPrefixSuffixPairs = prefixSuffixPairs
    +        .filter(x => frequentItemsMap.contains(x._1))
    +        .flatMap { case (prefix, suffix) =>
    +        val frequentItemSet = frequentItemsMap(prefix)
    +        val filteredSuffix = suffix.filter(frequentItemSet.contains(_))
    +        val nextSuffixes = frequentItemSet.map{ item =>
    --- End diff --
    
    Space before `{`.
    
    Can also do this in one pass
    ```scala
    frequentItemSet.flatMap { item =>
      val suffix = LocalPrefixSpan.getSuffix(item, filteredSuffix)
      if (suffix.isEmpty) None
      else Some(prefix :+ item, suffix)
    }
    ```


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


Mime
View raw message