You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by Syrux <gi...@git.apache.org> on 2017/04/08 10:26:58 UTC

[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

GitHub user Syrux opened a pull request:

    https://github.com/apache/spark/pull/17575

    [SPARK-20265][MLlib] Improve Prefix'span pre-processing efficiency

    ## What changes were proposed in this pull request?
    
    Improve PrefixSpan pre-processing efficency by preventing sequences of zero in the cleaned database. 
    The efficiency gain is reflected in the following graph : https://postimg.org/image/9x6ireuvn/
    
    ## How was this patch tested?
    
    Using MLlib's PrefixSpan existing tests and tests of my own on the 8 datasets shown in the graph. All
    result obtained were stricly the same as the original implementation (without this change).
    dev/run-tests was also runned, no error were found.
    
    Author : Cyril de Vogelaere <cy...@gmail.com>


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/Syrux/spark SPARK-20265

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/17575.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #17575
    
----
commit 7af4945fbfb309f7a7784cba2b1fc4cb4945fba0
Author: Syrux <po...@hotmail.com>
Date:   2017-04-08T10:17:04Z

    [SPARK-20265][MLlib] Improve Prefix'span pre-processing efficiency

----


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


[GitHub] spark issue #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-processing ...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17575
  
    **[Test build #3661 has started](https://amplab.cs.berkeley.edu/jenkins/job/NewSparkPullRequestBuilder/3661/testReport)** for PR 17575 at commit [`d799d46`](https://github.com/apache/spark/commit/d799d460e215c017b4385e8ecbbca8b92128096a).


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by Syrux <gi...@git.apache.org>.
Github user Syrux commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110662589
  
    --- Diff: mllib/src/test/scala/org/apache/spark/mllib/fpm/PrefixSpanSuite.scala ---
    @@ -360,6 +360,55 @@ class PrefixSpanSuite extends SparkFunSuite with MLlibTestSparkContext {
         compareResults(expected, model.freqSequences.collect())
       }
     
    +  test("PrefixSpan pre-processing's cleaning test") {
    +
    +    // One item per itemSet
    +    val itemToInt1 = (4 to 5).zipWithIndex.toMap
    +    val sequences1 = Seq(
    +      Array(Array(4), Array(1), Array(2), Array(5), Array(2), Array(4), Array(5)),
    +      Array(Array(6), Array(7), Array(8)))
    +    val rdd1 = sc.parallelize(sequences1, 2).cache()
    +
    +    val cleanedSequence1 = PrefixSpan.toDatabaseInternalRepr(rdd1, itemToInt1).collect()
    +
    +    val expected1 = Array(Array(0, 4, 0, 5, 0, 4, 0, 5, 0))
    +      .map(x => x.map(y => {
    +        if (y == 0) 0
    --- End diff --
    
    Ok, changing it


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110600704
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,68 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                     minCount : Long): Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    +      itemsets.foreach { _.foreach { item =>
    +        uniqItems += item
    +      }}
    +      uniqItems.toIterator.map((_, 1L))
    +    }.reduceByKey(_ + _).filter { case (_, count) =>
    +        count >= minCount
    +    }.sortBy(-_._2).map(_._1).collect()
    +  }
    +
    +  /**
    +   * This methods cleans the input dataset from un-frequent items, and translate it's item
    +   * to their corresponding Int identifier.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param itemToInt A map allowing translation of frequent Items to their Int Identifier.
    +   *                  The map should only contain frequent item.
    +   *
    +   * @return The internal repr of the inputted dataset. With properly placed zero delimiter.
    +   */
    +  private[fpm] def toDatabaseInternalRepr[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                        itemToInt : Map[Item, Int]):
    +  RDD[Array[Int]] = {
    +
    +    data.flatMap { itemsets =>
    +      val allItems = mutable.ArrayBuilder.make[Int]
    +      var containsFreqItems = false
    +      allItems += 0
    +      itemsets.foreach { itemsets =>
    +        val items = mutable.ArrayBuilder.make[Int]
    +        itemsets.foreach { item =>
    +          if (itemToInt.contains(item)) {
    +            items += itemToInt(item) + 1 // using 1-indexing in internal format
    +          }
    +        }
    +        val result = items.result()
    +        if (result.nonEmpty) {
    +          containsFreqItems = true
    +          allItems ++= result.sorted
    +          allItems += 0
    +        }
    +      }
    +      if (containsFreqItems) {
    --- End diff --
    
    Is this the same as checking `allItems.size > 1` now, rather than maintain a flag?


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by Syrux <gi...@git.apache.org>.
Github user Syrux commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110662506
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,68 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                     minCount : Long): Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    --- End diff --
    
    Ok, changed the accolades to parenthesis. (I suppose that what you meant, correct me if I'm wrong)
    Also, just by curiosity, do you know if that make any differences in performances ?


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


[GitHub] spark issue #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-processing ...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17575
  
    **[Test build #3658 has started](https://amplab.cs.berkeley.edu/jenkins/job/NewSparkPullRequestBuilder/3658/testReport)** for PR 17575 at commit [`627bfe0`](https://github.com/apache/spark/commit/627bfe0b04180a9c7248df6a3af519f15261faa6).


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110600152
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,68 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                     minCount : Long): Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    --- End diff --
    
    While you're here you're welcome to write `itemsets.foreach(_.foreach(item => uniqItems += item))`


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110671100
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,68 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                     minCount : Long): Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    +      itemsets.foreach { _.foreach { item =>
    +        uniqItems += item
    +      }}
    +      uniqItems.toIterator.map((_, 1L))
    +    }.reduceByKey(_ + _).filter { case (_, count) =>
    +        count >= minCount
    +    }.sortBy(-_._2).map(_._1).collect()
    +  }
    +
    +  /**
    +   * This methods cleans the input dataset from un-frequent items, and translate it's item
    +   * to their corresponding Int identifier.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param itemToInt A map allowing translation of frequent Items to their Int Identifier.
    +   *                  The map should only contain frequent item.
    +   *
    +   * @return The internal repr of the inputted dataset. With properly placed zero delimiter.
    +   */
    +  private[fpm] def toDatabaseInternalRepr[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                        itemToInt : Map[Item, Int]):
    +  RDD[Array[Int]] = {
    +
    +    data.flatMap { itemsets =>
    +      val allItems = mutable.ArrayBuilder.make[Int]
    +      var containsFreqItems = false
    +      allItems += 0
    +      itemsets.foreach { itemsets =>
    +        val items = mutable.ArrayBuilder.make[Int]
    +        itemsets.foreach { item =>
    +          if (itemToInt.contains(item)) {
    +            items += itemToInt(item) + 1 // using 1-indexing in internal format
    +          }
    +        }
    +        val result = items.result()
    +        if (result.nonEmpty) {
    +          containsFreqItems = true
    +          allItems ++= result.sorted
    +          allItems += 0
    +        }
    +      }
    +      if (containsFreqItems) {
    --- End diff --
    
    OK no problem, leave it. Just riffing while we're editing the code.


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110827907
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,69 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](
    +      data: RDD[Array[Array[Item]]],
    +      minCount: Long):
    +  Array[Item] = {
    --- End diff --
    
    Wrap this onto the previous line


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by Syrux <gi...@git.apache.org>.
Github user Syrux commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110839623
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,69 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](
    +      data: RDD[Array[Array[Item]]],
    +      minCount: Long):
    +  Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    +      itemsets.foreach(set => uniqItems ++= set)
    +      uniqItems.toIterator.map((_, 1L))
    +    }.reduceByKey(_ + _).filter { case (_, count) =>
    +        count >= minCount
    --- End diff --
    
    Ok


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


[GitHub] spark issue #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-processing ...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17575
  
    Can one of the admins verify this patch?


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by Syrux <gi...@git.apache.org>.
Github user Syrux commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110664282
  
    --- Diff: mllib/src/test/scala/org/apache/spark/mllib/fpm/PrefixSpanSuite.scala ---
    @@ -360,6 +360,55 @@ class PrefixSpanSuite extends SparkFunSuite with MLlibTestSparkContext {
         compareResults(expected, model.freqSequences.collect())
       }
     
    +  test("PrefixSpan pre-processing's cleaning test") {
    +
    +    // One item per itemSet
    +    val itemToInt1 = (4 to 5).zipWithIndex.toMap
    +    val sequences1 = Seq(
    +      Array(Array(4), Array(1), Array(2), Array(5), Array(2), Array(4), Array(5)),
    +      Array(Array(6), Array(7), Array(8)))
    +    val rdd1 = sc.parallelize(sequences1, 2).cache()
    +
    +    val cleanedSequence1 = PrefixSpan.toDatabaseInternalRepr(rdd1, itemToInt1).collect()
    +
    +    val expected1 = Array(Array(0, 4, 0, 5, 0, 4, 0, 5, 0))
    +      .map(x => x.map(y => {
    +        if (y == 0) 0
    +        else itemToInt1(y) + 1
    +      }))
    +
    +    compareInternalSequences(expected1, cleanedSequence1)
    +
    +    // Multi-item sequence
    +    val itemToInt2 = (4 to 6).zipWithIndex.toMap
    +    val sequences2 = Seq(
    +      Array(Array(4, 5), Array(1, 6, 2), Array(2), Array(5), Array(2), Array(4), Array(5, 6, 7)),
    +      Array(Array(8, 9), Array(1, 2)))
    +    val rdd2 = sc.parallelize(sequences2, 2).cache()
    +
    +    val cleanedSequence2 = PrefixSpan.toDatabaseInternalRepr(rdd2, itemToInt2).collect()
    +
    +    val expected2 = Array(Array(0, 4, 5, 0, 6, 0, 5, 0, 4, 0, 5, 6, 0))
    +      .map(x => x.map(y => {
    +        if (y == 0) 0
    +        else itemToInt2(y) + 1
    +      }))
    +
    +    compareInternalSequences(expected2, cleanedSequence2)
    +
    +    // Emptied sequence
    +    val itemToInt3 = (10 to 10).zipWithIndex.toMap
    +    val sequences3 = Seq(
    +      Array(Array(4, 5), Array(1, 6, 2), Array(2), Array(5), Array(2), Array(4), Array(5, 6, 7)),
    +      Array(Array(8, 9), Array(1, 2)))
    +    val rdd3 = sc.parallelize(sequences3, 2).cache()
    +
    +    val cleanedSequence3 = PrefixSpan.toDatabaseInternalRepr(rdd3, itemToInt3).collect()
    +    val expected3: Array[Array[Int]] = Array()
    --- End diff --
    
    Yep, it can. It even avoids a useless cast.
    I will push the new version asap


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


[GitHub] spark issue #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-processing ...

Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on the issue:

    https://github.com/apache/spark/pull/17575
  
    Merged to master


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110599784
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,68 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                     minCount : Long): Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    +      itemsets.foreach { _.foreach { item =>
    +        uniqItems += item
    +      }}
    +      uniqItems.toIterator.map((_, 1L))
    +    }.reduceByKey(_ + _).filter { case (_, count) =>
    +        count >= minCount
    +    }.sortBy(-_._2).map(_._1).collect()
    +  }
    +
    +  /**
    +   * This methods cleans the input dataset from un-frequent items, and translate it's item
    +   * to their corresponding Int identifier.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param itemToInt A map allowing translation of frequent Items to their Int Identifier.
    +   *                  The map should only contain frequent item.
    +   *
    +   * @return The internal repr of the inputted dataset. With properly placed zero delimiter.
    +   */
    +  private[fpm] def toDatabaseInternalRepr[Item: ClassTag](data : RDD[Array[Array[Item]]],
    --- End diff --
    
    We generally start wrapping the args to 4-space indent here in this case. Then you can pull up the return type. Also no space before colon


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110662125
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,68 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                     minCount : Long): Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    --- End diff --
    
    or does `itemsets.foreach(set => uniqItems ++= set)` 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 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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110662332
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,68 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                     minCount : Long): Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    +      itemsets.foreach { _.foreach { item =>
    +        uniqItems += item
    +      }}
    +      uniqItems.toIterator.map((_, 1L))
    +    }.reduceByKey(_ + _).filter { case (_, count) =>
    +        count >= minCount
    +    }.sortBy(-_._2).map(_._1).collect()
    +  }
    +
    +  /**
    +   * This methods cleans the input dataset from un-frequent items, and translate it's item
    +   * to their corresponding Int identifier.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param itemToInt A map allowing translation of frequent Items to their Int Identifier.
    +   *                  The map should only contain frequent item.
    +   *
    +   * @return The internal repr of the inputted dataset. With properly placed zero delimiter.
    +   */
    +  private[fpm] def toDatabaseInternalRepr[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                        itemToInt : Map[Item, Int]):
    +  RDD[Array[Int]] = {
    +
    +    data.flatMap { itemsets =>
    +      val allItems = mutable.ArrayBuilder.make[Int]
    +      var containsFreqItems = false
    +      allItems += 0
    +      itemsets.foreach { itemsets =>
    +        val items = mutable.ArrayBuilder.make[Int]
    +        itemsets.foreach { item =>
    +          if (itemToInt.contains(item)) {
    +            items += itemToInt(item) + 1 // using 1-indexing in internal format
    +          }
    +        }
    +        val result = items.result()
    +        if (result.nonEmpty) {
    +          containsFreqItems = true
    +          allItems ++= result.sorted
    +          allItems += 0
    +        }
    +      }
    +      if (containsFreqItems) {
    --- End diff --
    
    I see. What about waiting to pre-pend the initial 0 until the end, only if not empty? 


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


[GitHub] spark issue #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-processing ...

Posted by Syrux <gi...@git.apache.org>.
Github user Syrux commented on the issue:

    https://github.com/apache/spark/pull/17575
  
    Ok, should I create a new Jira and push there the additionnal tests ?
    Or is here completly fine, since it's related to the current change
    
    Tell me, and I will get the change done asap :)


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


[GitHub] spark issue #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-processing ...

Posted by Syrux <gi...@git.apache.org>.
Github user Syrux commented on the issue:

    https://github.com/apache/spark/pull/17575
  
    Yo Sean, I already pushed the requested changes in case it's the correct place to do so.
    (I can just revert them, if not)
    
    I added two new methods to allow tests. First a method which finds all frequent items in a database, second a method that actually clean the database using those frequent items. Although I didn't end up using the first method, the pre-processing method is now much clearer to understand. So I left the new method. Just tell me if I need to put that piece of code back.
    
    I also added tests for multiple types of sequence database. More specifically, when there is max one item per itemset, when there can be multiple items per itemsets, and when cleaning the database empties it. They should cover all cases together.
    
    Of course, the new implementation passes the tests perfectly, and the old one doesn't.
    Every other thing remained as is.
    
    Tell me if the way I did it was ok. I hope it's up to standards :)


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


[GitHub] spark issue #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-processing ...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17575
  
    **[Test build #3651 has started](https://amplab.cs.berkeley.edu/jenkins/job/NewSparkPullRequestBuilder/3651/testReport)** for PR 17575 at commit [`8e5db6a`](https://github.com/apache/spark/commit/8e5db6af95545121d379dbca83bedc23cbd5e6c0).


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by Syrux <gi...@git.apache.org>.
Github user Syrux commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110669561
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,68 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                     minCount : Long): Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    +      itemsets.foreach { _.foreach { item =>
    +        uniqItems += item
    +      }}
    +      uniqItems.toIterator.map((_, 1L))
    +    }.reduceByKey(_ + _).filter { case (_, count) =>
    +        count >= minCount
    +    }.sortBy(-_._2).map(_._1).collect()
    +  }
    +
    +  /**
    +   * This methods cleans the input dataset from un-frequent items, and translate it's item
    +   * to their corresponding Int identifier.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param itemToInt A map allowing translation of frequent Items to their Int Identifier.
    +   *                  The map should only contain frequent item.
    +   *
    +   * @return The internal repr of the inputted dataset. With properly placed zero delimiter.
    +   */
    +  private[fpm] def toDatabaseInternalRepr[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                        itemToInt : Map[Item, Int]):
    +  RDD[Array[Int]] = {
    +
    +    data.flatMap { itemsets =>
    +      val allItems = mutable.ArrayBuilder.make[Int]
    +      var containsFreqItems = false
    +      allItems += 0
    +      itemsets.foreach { itemsets =>
    +        val items = mutable.ArrayBuilder.make[Int]
    +        itemsets.foreach { item =>
    +          if (itemToInt.contains(item)) {
    +            items += itemToInt(item) + 1 // using 1-indexing in internal format
    +          }
    +        }
    +        val result = items.result()
    +        if (result.nonEmpty) {
    +          containsFreqItems = true
    +          allItems ++= result.sorted
    +          allItems += 0
    +        }
    +      }
    +      if (containsFreqItems) {
    --- End diff --
    
    Apparently, prepending is impossible on an arrayBuilder. The method doesn't exist (http://www.scala-lang.org/api/2.12.0/scala/collection/mutable/ArrayBuilder.html).
    I think the flag is our best bet for performance. Changing it to an arrayBuffer would be far worse since a type encapsulation would be forced on the ints it contain.


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


[GitHub] spark issue #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-processing ...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17575
  
    **[Test build #3658 has finished](https://amplab.cs.berkeley.edu/jenkins/job/NewSparkPullRequestBuilder/3658/testReport)** for PR 17575 at commit [`627bfe0`](https://github.com/apache/spark/commit/627bfe0b04180a9c7248df6a3af519f15261faa6).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


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


[GitHub] spark issue #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-processing ...

Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on the issue:

    https://github.com/apache/spark/pull/17575
  
    Even a simplistic test of this case would give a lot more confidence that it's correct. If it means opening up a `private[spark]` method or two to make testing possible that seems reasonable. I don' think it needs significant change. Something needs to exercise this code path.


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110827932
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,69 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](
    +      data: RDD[Array[Array[Item]]],
    +      minCount: Long):
    +  Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    +      itemsets.foreach(set => uniqItems ++= set)
    +      uniqItems.toIterator.map((_, 1L))
    +    }.reduceByKey(_ + _).filter { case (_, count) =>
    +        count >= minCount
    --- End diff --
    
    Nit, but this should unindent one unit


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110600408
  
    --- Diff: mllib/src/test/scala/org/apache/spark/mllib/fpm/PrefixSpanSuite.scala ---
    @@ -360,6 +360,55 @@ class PrefixSpanSuite extends SparkFunSuite with MLlibTestSparkContext {
         compareResults(expected, model.freqSequences.collect())
       }
     
    +  test("PrefixSpan pre-processing's cleaning test") {
    +
    +    // One item per itemSet
    +    val itemToInt1 = (4 to 5).zipWithIndex.toMap
    +    val sequences1 = Seq(
    +      Array(Array(4), Array(1), Array(2), Array(5), Array(2), Array(4), Array(5)),
    +      Array(Array(6), Array(7), Array(8)))
    +    val rdd1 = sc.parallelize(sequences1, 2).cache()
    +
    +    val cleanedSequence1 = PrefixSpan.toDatabaseInternalRepr(rdd1, itemToInt1).collect()
    +
    +    val expected1 = Array(Array(0, 4, 0, 5, 0, 4, 0, 5, 0))
    +      .map(x => x.map(y => {
    +        if (y == 0) 0
    --- End diff --
    
    I'd put this on one line


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


[GitHub] spark issue #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-processing ...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17575
  
    **[Test build #3661 has finished](https://amplab.cs.berkeley.edu/jenkins/job/NewSparkPullRequestBuilder/3661/testReport)** for PR 17575 at commit [`d799d46`](https://github.com/apache/spark/commit/d799d460e215c017b4385e8ecbbca8b92128096a).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by Syrux <gi...@git.apache.org>.
Github user Syrux commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110661386
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,68 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                     minCount : Long): Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    +      itemsets.foreach { _.foreach { item =>
    +        uniqItems += item
    +      }}
    +      uniqItems.toIterator.map((_, 1L))
    +    }.reduceByKey(_ + _).filter { case (_, count) =>
    +        count >= minCount
    +    }.sortBy(-_._2).map(_._1).collect()
    +  }
    +
    +  /**
    +   * This methods cleans the input dataset from un-frequent items, and translate it's item
    +   * to their corresponding Int identifier.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param itemToInt A map allowing translation of frequent Items to their Int Identifier.
    +   *                  The map should only contain frequent item.
    +   *
    +   * @return The internal repr of the inputted dataset. With properly placed zero delimiter.
    +   */
    +  private[fpm] def toDatabaseInternalRepr[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                        itemToInt : Map[Item, Int]):
    +  RDD[Array[Int]] = {
    +
    +    data.flatMap { itemsets =>
    +      val allItems = mutable.ArrayBuilder.make[Int]
    +      var containsFreqItems = false
    +      allItems += 0
    +      itemsets.foreach { itemsets =>
    +        val items = mutable.ArrayBuilder.make[Int]
    +        itemsets.foreach { item =>
    +          if (itemToInt.contains(item)) {
    +            items += itemToInt(item) + 1 // using 1-indexing in internal format
    +          }
    +        }
    +        val result = items.result()
    +        if (result.nonEmpty) {
    +          containsFreqItems = true
    +          allItems ++= result.sorted
    +          allItems += 0
    +        }
    +      }
    +      if (containsFreqItems) {
    --- End diff --
    
    Yes, but allItems is an arrayBuilder, so there is no size method.
    I could do allIItems.result().size but I think the performance might be worse than a flag.


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by Syrux <gi...@git.apache.org>.
Github user Syrux commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110661717
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,68 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                     minCount : Long): Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    +      itemsets.foreach { _.foreach { item =>
    +        uniqItems += item
    +      }}
    +      uniqItems.toIterator.map((_, 1L))
    +    }.reduceByKey(_ + _).filter { case (_, count) =>
    +        count >= minCount
    +    }.sortBy(-_._2).map(_._1).collect()
    +  }
    +
    +  /**
    +   * This methods cleans the input dataset from un-frequent items, and translate it's item
    +   * to their corresponding Int identifier.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param itemToInt A map allowing translation of frequent Items to their Int Identifier.
    +   *                  The map should only contain frequent item.
    +   *
    +   * @return The internal repr of the inputted dataset. With properly placed zero delimiter.
    +   */
    +  private[fpm] def toDatabaseInternalRepr[Item: ClassTag](data : RDD[Array[Array[Item]]],
    --- End diff --
    
    Ok, the new version will fix that and the colon space.


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110600533
  
    --- Diff: mllib/src/test/scala/org/apache/spark/mllib/fpm/PrefixSpanSuite.scala ---
    @@ -360,6 +360,55 @@ class PrefixSpanSuite extends SparkFunSuite with MLlibTestSparkContext {
         compareResults(expected, model.freqSequences.collect())
       }
     
    +  test("PrefixSpan pre-processing's cleaning test") {
    +
    +    // One item per itemSet
    +    val itemToInt1 = (4 to 5).zipWithIndex.toMap
    +    val sequences1 = Seq(
    +      Array(Array(4), Array(1), Array(2), Array(5), Array(2), Array(4), Array(5)),
    +      Array(Array(6), Array(7), Array(8)))
    +    val rdd1 = sc.parallelize(sequences1, 2).cache()
    +
    +    val cleanedSequence1 = PrefixSpan.toDatabaseInternalRepr(rdd1, itemToInt1).collect()
    +
    +    val expected1 = Array(Array(0, 4, 0, 5, 0, 4, 0, 5, 0))
    +      .map(x => x.map(y => {
    +        if (y == 0) 0
    +        else itemToInt1(y) + 1
    +      }))
    +
    +    compareInternalSequences(expected1, cleanedSequence1)
    +
    +    // Multi-item sequence
    +    val itemToInt2 = (4 to 6).zipWithIndex.toMap
    +    val sequences2 = Seq(
    +      Array(Array(4, 5), Array(1, 6, 2), Array(2), Array(5), Array(2), Array(4), Array(5, 6, 7)),
    +      Array(Array(8, 9), Array(1, 2)))
    +    val rdd2 = sc.parallelize(sequences2, 2).cache()
    +
    +    val cleanedSequence2 = PrefixSpan.toDatabaseInternalRepr(rdd2, itemToInt2).collect()
    +
    +    val expected2 = Array(Array(0, 4, 5, 0, 6, 0, 5, 0, 4, 0, 5, 6, 0))
    +      .map(x => x.map(y => {
    +        if (y == 0) 0
    +        else itemToInt2(y) + 1
    +      }))
    +
    +    compareInternalSequences(expected2, cleanedSequence2)
    +
    +    // Emptied sequence
    +    val itemToInt3 = (10 to 10).zipWithIndex.toMap
    +    val sequences3 = Seq(
    +      Array(Array(4, 5), Array(1, 6, 2), Array(2), Array(5), Array(2), Array(4), Array(5, 6, 7)),
    +      Array(Array(8, 9), Array(1, 2)))
    +    val rdd3 = sc.parallelize(sequences3, 2).cache()
    +
    +    val cleanedSequence3 = PrefixSpan.toDatabaseInternalRepr(rdd3, itemToInt3).collect()
    +    val expected3: Array[Array[Int]] = Array()
    --- End diff --
    
    Nit: can this be `val expected3 = Array[Array[Int]]()`?


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


[GitHub] spark issue #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-processing ...

Posted by Syrux <gi...@git.apache.org>.
Github user Syrux commented on the issue:

    https://github.com/apache/spark/pull/17575
  
    Yes exactly, the current implementation adds too much unnecessary delimiters. We this one line change, delimiter are only placed where needed. 
    
    Currently there are no tests to verify if the algorithm cleans the sequences correctly. I only found that inneficiency by printing stuff around while I implemented other things on my local github. 
    
    If you want, I can add some tests, but that will necessitate a small refector to separate the cleaning part in it's own method. Calling the current method would directly call the main algorithm ... ^^'
    
    Two of the existing tests did cover cases where sequence of zero where left. However not at pertinent places (Integer/String type, variable-size itemsets clean a five at the end of the third sequence, leaving 2 zero instead of one). 
    
    I can however vouch that the previous code worked just fine. Both the results of the old implementation and this one are the same. They also correspond to the results I obtained for another standalone CP based implementation. It's just that this code makes the pre-processing more efficient.


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by Syrux <gi...@git.apache.org>.
Github user Syrux commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110671916
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,68 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                     minCount : Long): Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    --- End diff --
    
    itemsets.foreach(set => uniqItems ++= set) does work. I will change it in my next commit. I will push it once I know what to do for the flag.


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by Syrux <gi...@git.apache.org>.
Github user Syrux commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110667171
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,68 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                     minCount : Long): Array[Item] = {
    +
    +    data.flatMap { itemsets =>
    +      val uniqItems = mutable.Set.empty[Item]
    +      itemsets.foreach { _.foreach { item =>
    +        uniqItems += item
    +      }}
    +      uniqItems.toIterator.map((_, 1L))
    +    }.reduceByKey(_ + _).filter { case (_, count) =>
    +        count >= minCount
    +    }.sortBy(-_._2).map(_._1).collect()
    +  }
    +
    +  /**
    +   * This methods cleans the input dataset from un-frequent items, and translate it's item
    +   * to their corresponding Int identifier.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param itemToInt A map allowing translation of frequent Items to their Int Identifier.
    +   *                  The map should only contain frequent item.
    +   *
    +   * @return The internal repr of the inputted dataset. With properly placed zero delimiter.
    +   */
    +  private[fpm] def toDatabaseInternalRepr[Item: ClassTag](data : RDD[Array[Array[Item]]],
    +                                                        itemToInt : Map[Item, Int]):
    +  RDD[Array[Int]] = {
    +
    +    data.flatMap { itemsets =>
    +      val allItems = mutable.ArrayBuilder.make[Int]
    +      var containsFreqItems = false
    +      allItems += 0
    +      itemsets.foreach { itemsets =>
    +        val items = mutable.ArrayBuilder.make[Int]
    +        itemsets.foreach { item =>
    +          if (itemToInt.contains(item)) {
    +            items += itemToInt(item) + 1 // using 1-indexing in internal format
    +          }
    +        }
    +        val result = items.result()
    +        if (result.nonEmpty) {
    +          containsFreqItems = true
    +          allItems ++= result.sorted
    +          allItems += 0
    +        }
    +      }
    +      if (containsFreqItems) {
    --- End diff --
    
    I am not sure about the performance of a pre-append on arrayBuilder. I will check them first. Back in a few minutes.


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/spark/pull/17575


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


[GitHub] spark pull request #17575: [SPARK-20265][MLlib] Improve Prefix'span pre-proc...

Posted by Syrux <gi...@git.apache.org>.
Github user Syrux commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17575#discussion_r110839571
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/fpm/PrefixSpan.scala ---
    @@ -232,6 +200,69 @@ class PrefixSpan private (
     object PrefixSpan extends Logging {
     
       /**
    +   * This methods finds all frequent items in a input dataset.
    +   *
    +   * @param data Sequences of itemsets.
    +   * @param minCount The minimal number of sequence an item should be present in to be frequent
    +   *
    +   * @return An array of Item containing only frequent items.
    +   */
    +  private[fpm] def findFrequentItems[Item: ClassTag](
    +      data: RDD[Array[Array[Item]]],
    +      minCount: Long):
    +  Array[Item] = {
    --- End diff --
    
    Ok, I will push the changes


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