|
| 1 | +--- |
| 2 | +layout: global |
| 3 | +title: Frequent Pattern Mining - MLlib |
| 4 | +displayTitle: <a href="mllib-guide.html">MLlib</a> - Frequent Pattern Mining |
| 5 | +--- |
| 6 | + |
| 7 | +Mining frequent items, itemsets, subsequences, or other substructures is usually among the |
| 8 | +first steps to analyze a large-scale dataset, which has been an active research topic in |
| 9 | +data mining for years. |
| 10 | +We refer users to Wikipedia's [association rule learning](http://en.wikipedia.org/wiki/Association_rule_learning) |
| 11 | +for more information. |
| 12 | +MLlib provides a parallel implementation of FP-growth, |
| 13 | +a popular algorithm to mining frequent itemsets. |
| 14 | + |
| 15 | +## FP-growth |
| 16 | + |
| 17 | +The FP-growth algorithm is described in the paper |
| 18 | +[Han et al., Mining frequent patterns without candidate generation](http://dx.doi.org/10.1145/335191.335372), |
| 19 | +where "FP" stands for frequent pattern. |
| 20 | +Given a dataset of transactions, the first step of FP-growth is to calculate item frequencies and identify frequent items. |
| 21 | +Different from [Apriori-like](http://en.wikipedia.org/wiki/Apriori_algorithm) algorithms designed for the same purpose, |
| 22 | +the second step of FP-growth uses a suffix tree (FP-tree) structure to encode transactions without generating candidate sets |
| 23 | +explicitly, which are usually expensive to generate. |
| 24 | +After the second step, the frequent itemsets can be extracted from the FP-tree. |
| 25 | +In MLlib, we implemented a parallel version of FP-growth called PFP, |
| 26 | +as described in [Li et al., PFP: Parallel FP-growth for query recommendation](http://dx.doi.org/10.1145/1454008.1454027). |
| 27 | +PFP distributes the work of growing FP-trees based on the suffices of transactions, |
| 28 | +and hence more scalable than a single-machine implementation. |
| 29 | +We refer users to the papers for more details. |
| 30 | + |
| 31 | +MLlib's FP-growth implementation takes the following (hyper-)parameters: |
| 32 | + |
| 33 | +* `minSupport`: the minimum support for an itemset to be identified as frequent. |
| 34 | + For example, if an item appears 3 out of 5 transactions, it has a support of 3/5=0.6. |
| 35 | +* `numPartitions`: the number of partitions used to distribute the work. |
| 36 | + |
| 37 | +**Examples** |
| 38 | + |
| 39 | +<div class="codetabs"> |
| 40 | +<div data-lang="scala" markdown="1"> |
| 41 | + |
| 42 | +[`FPGrowth`](api/java/org/apache/spark/mllib/fpm/FPGrowth.html) implements the |
| 43 | +FP-growth algorithm. |
| 44 | +It take a `JavaRDD` of transactions, where each transaction is an `Iterable` of items of a generic type. |
| 45 | +Calling `FPGrowth.run` with transactions returns an |
| 46 | +[`FPGrowthModel`](api/java/org/apache/spark/mllib/fpm/FPGrowthModel.html) |
| 47 | +that stores the frequent itemsets with their frequencies. |
| 48 | + |
| 49 | +{% highlight scala %} |
| 50 | +import org.apache.spark.rdd.RDD |
| 51 | +import org.apache.spark.mllib.fpm.{FPGrowth, FPGrowthModel} |
| 52 | + |
| 53 | +val transactions: RDD[Array[String]] = ... |
| 54 | + |
| 55 | +val fpg = new FPGrowth() |
| 56 | + .setMinSupport(0.2) |
| 57 | + .setNumPartitions(10) |
| 58 | +val model = fpg.run(transactions) |
| 59 | + |
| 60 | +model.freqItemsets.collect().foreach { case (itemset, freq) => |
| 61 | + println(itemset.mkString("[", ",", "]") + ", " + freq) |
| 62 | +} |
| 63 | +{% endhighlight %} |
| 64 | + |
| 65 | +</div> |
| 66 | + |
| 67 | +<div data-lang="java" markdown="1"> |
| 68 | + |
| 69 | +[`FPGrowth`](api/java/org/apache/spark/mllib/fpm/FPGrowth.html) implements the |
| 70 | +FP-growth algorithm. |
| 71 | +It take an `RDD` of transactions, where each transaction is an `Array` of items of a generic type. |
| 72 | +Calling `FPGrowth.run` with transactions returns an |
| 73 | +[`FPGrowthModel`](api/java/org/apache/spark/mllib/fpm/FPGrowthModel.html) |
| 74 | +that stores the frequent itemsets with their frequencies. |
| 75 | + |
| 76 | +{% highlight java %} |
| 77 | +import java.util.Arrays; |
| 78 | +import java.util.List; |
| 79 | + |
| 80 | +import scala.Tuple2; |
| 81 | + |
| 82 | +import org.apache.spark.api.java.JavaRDD; |
| 83 | +import org.apache.spark.mllib.fpm.FPGrowth; |
| 84 | +import org.apache.spark.mllib.fpm.FPGrowthModel; |
| 85 | + |
| 86 | +JavaRDD<List<String>> transactions = ... |
| 87 | + |
| 88 | +FPGrowth fpg = new FPGrowth() |
| 89 | + .setMinSupport(0.2) |
| 90 | + .setNumPartitions(10); |
| 91 | + |
| 92 | +FPGrowthModel<String> model = fpg.run(transactions); |
| 93 | + |
| 94 | +for (Tuple2<Object, Long> s: model.javaFreqItemsets().collect()) { |
| 95 | + System.out.println("(" + Arrays.toString((Object[]) s._1()) + "): " + s._2()); |
| 96 | +} |
| 97 | +{% endhighlight %} |
| 98 | + |
| 99 | +</div> |
| 100 | +</div> |
0 commit comments