Github user zhangjiajin commented on a diff in the pull request:
https://github.com/apache/spark/pull/7412#discussion_r34657699
--- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala
---
@@ -86,16 +88,69 @@ class PrefixSpan private (
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
+
+ var patternsCount = lengthOnePatternsAndCounts.length
+ var allPatternAndCounts = sequences.sparkContext.parallelize(
+ lengthOnePatternsAndCounts.map(x => (Array(x._1), x._2)))
+ var currentProjectedDatabase = prefixAndProjectedDatabase
+ while (patternsCount <= minPatternsBeforeShuffle &&
+ currentProjectedDatabase.count() != 0) {
+ val (nextPatternAndCounts, nextProjectedDatabase) =
+ getPatternCountsAndProjectedDatabase(minCount,
currentProjectedDatabase)
+ patternsCount = nextPatternAndCounts.count().toInt
+ currentProjectedDatabase = nextProjectedDatabase
+ allPatternAndCounts = allPatternAndCounts ++ nextPatternAndCounts
+ }
+ if (patternsCount > 0) {
+ val groupedProjectedDatabase = currentProjectedDatabase
+ .map(x => (x._1.toSeq, x._2))
+ .groupByKey()
+ .map(x => (x._1.toArray, x._2.toArray))
+ val nextPatternAndCounts = getPatternsInLocal(minCount,
groupedProjectedDatabase)
+ allPatternAndCounts = allPatternAndCounts ++ nextPatternAndCounts
+ }
+ allPatternAndCounts
+ }
+
+ /**
+ * Get the pattern and counts, and projected database
+ * @param minCount minimum count
+ * @param prefixAndProjectedDatabase prefix and projected database,
+ * @return pattern and counts, and projected database
+ * (Array[pattern, count], RDD[prefix, projected database ])
+ */
+ private def getPatternCountsAndProjectedDatabase(
+ minCount: Long,
+ prefixAndProjectedDatabase: RDD[(Array[Int], Array[Int])]):
+ (RDD[(Array[Int], Long)], RDD[(Array[Int], Array[Int])]) = {
+ val prefixAndFreqentItemAndCounts =
prefixAndProjectedDatabase.flatMap{ x =>
+ x._2.distinct.map(y => ((x._1.toSeq, y), 1L))
+ }.reduceByKey(_ + _)
+ .filter(_._2 >= minCount)
+ val patternAndCounts = prefixAndFreqentItemAndCounts
+ .map(x => (x._1._1.toArray ++ Array(x._1._2), x._2))
+ val prefixlength = prefixAndProjectedDatabase.take(1)(0)._1.length
--- End diff --
It is not easy that we move prefix length check into the loop on L96.
The current process:
1. get length-1 patterns
2. get length-1 prefix suffix pairs
3. while ( (current patterns count < minPatternsBeforeShuffle) and (prefix
suffix pairs is not empty) ) {
4. get next pattern patterns and prefix suffix pairs
5. }
6. shuffle and next work
if we move the check into the loop, we must split
getPatternCountsAndPrefixSuffixPairs() to two method: getPatternCounts() and
getPrefixSuffixPairs(),
ps. the method of get length-1 patterns and prefix suffix pairs are
different from get length-n (n>1) patterns and prefix suffix pairs.
1. get length-1 patterns
2. get length-1 prefix suffix pairs
3. get length-2 patterns ( call new method getPatternCounts() )
4. while ( (current patterns count < minPatternsBeforeShuffle) and (prefix
suffix pairs is not empty) and (pattern length < maxPatternLength) ) {
5. get next prefix suffix pairs
6. get next pattern patterns
7. }
8. get lenght-n+1 suffix pairs for shuffle
9. shuffle and next work
---
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]