0001 ---
0002 layout: global
0003 title: Frequent Pattern Mining
0004 displayTitle: Frequent Pattern Mining
0005 license: |
0006 Licensed to the Apache Software Foundation (ASF) under one or more
0007 contributor license agreements. See the NOTICE file distributed with
0008 this work for additional information regarding copyright ownership.
0009 The ASF licenses this file to You under the Apache License, Version 2.0
0010 (the "License"); you may not use this file except in compliance with
0011 the License. You may obtain a copy of the License at
0012
0013 http://www.apache.org/licenses/LICENSE-2.0
0014
0015 Unless required by applicable law or agreed to in writing, software
0016 distributed under the License is distributed on an "AS IS" BASIS,
0017 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0018 See the License for the specific language governing permissions and
0019 limitations under the License.
0020 ---
0021
0022 Mining frequent items, itemsets, subsequences, or other substructures is usually among the
0023 first steps to analyze a large-scale dataset, which has been an active research topic in
0024 data mining for years.
0025 We refer users to Wikipedia's [association rule learning](http://en.wikipedia.org/wiki/Association_rule_learning)
0026 for more information.
0027
0028 **Table of Contents**
0029
0030 * This will become a table of contents (this text will be scraped).
0031 {:toc}
0032
0033 ## FP-Growth
0034
0035 The FP-growth algorithm is described in the paper
0036 [Han et al., Mining frequent patterns without candidate generation](https://doi.org/10.1145/335191.335372),
0037 where "FP" stands for frequent pattern.
0038 Given a dataset of transactions, the first step of FP-growth is to calculate item frequencies and identify frequent items.
0039 Different from [Apriori-like](http://en.wikipedia.org/wiki/Apriori_algorithm) algorithms designed for the same purpose,
0040 the second step of FP-growth uses a suffix tree (FP-tree) structure to encode transactions without generating candidate sets
0041 explicitly, which are usually expensive to generate.
0042 After the second step, the frequent itemsets can be extracted from the FP-tree.
0043 In `spark.mllib`, we implemented a parallel version of FP-growth called PFP,
0044 as described in [Li et al., PFP: Parallel FP-growth for query recommendation](https://doi.org/10.1145/1454008.1454027).
0045 PFP distributes the work of growing FP-trees based on the suffixes of transactions,
0046 and hence is more scalable than a single-machine implementation.
0047 We refer users to the papers for more details.
0048
0049 `spark.ml`'s FP-growth implementation takes the following (hyper-)parameters:
0050
0051 * `minSupport`: the minimum support for an itemset to be identified as frequent.
0052 For example, if an item appears 3 out of 5 transactions, it has a support of 3/5=0.6.
0053 * `minConfidence`: minimum confidence for generating Association Rule. Confidence is an indication of how often an
0054 association rule has been found to be true. For example, if in the transactions itemset `X` appears 4 times, `X`
0055 and `Y` co-occur only 2 times, the confidence for the rule `X => Y` is then 2/4 = 0.5. The parameter will not
0056 affect the mining for frequent itemsets, but specify the minimum confidence for generating association rules
0057 from frequent itemsets.
0058 * `numPartitions`: the number of partitions used to distribute the work. By default the param is not set, and
0059 number of partitions of the input dataset is used.
0060
0061 The `FPGrowthModel` provides:
0062
0063 * `freqItemsets`: frequent itemsets in the format of DataFrame("items"[Array], "freq"[Long])
0064 * `associationRules`: association rules generated with confidence above `minConfidence`, in the format of
0065 DataFrame("antecedent"[Array], "consequent"[Array], "confidence"[Double]).
0066 * `transform`: For each transaction in `itemsCol`, the `transform` method will compare its items against the antecedents
0067 of each association rule. If the record contains all the antecedents of a specific association rule, the rule
0068 will be considered as applicable and its consequents will be added to the prediction result. The transform
0069 method will summarize the consequents from all the applicable rules as prediction. The prediction column has
0070 the same data type as `itemsCol` and does not contain existing items in the `itemsCol`.
0071
0072
0073 **Examples**
0074
0075 <div class="codetabs">
0076
0077 <div data-lang="scala" markdown="1">
0078 Refer to the [Scala API docs](api/scala/org/apache/spark/ml/fpm/FPGrowth.html) for more details.
0079
0080 {% include_example scala/org/apache/spark/examples/ml/FPGrowthExample.scala %}
0081 </div>
0082
0083 <div data-lang="java" markdown="1">
0084 Refer to the [Java API docs](api/java/org/apache/spark/ml/fpm/FPGrowth.html) for more details.
0085
0086 {% include_example java/org/apache/spark/examples/ml/JavaFPGrowthExample.java %}
0087 </div>
0088
0089 <div data-lang="python" markdown="1">
0090 Refer to the [Python API docs](api/python/pyspark.ml.html#pyspark.ml.fpm.FPGrowth) for more details.
0091
0092 {% include_example python/ml/fpgrowth_example.py %}
0093 </div>
0094
0095 <div data-lang="r" markdown="1">
0096
0097 Refer to the [R API docs](api/R/spark.fpGrowth.html) for more details.
0098
0099 {% include_example r/ml/fpm.R %}
0100 </div>
0101
0102 </div>
0103
0104 ## PrefixSpan
0105
0106 PrefixSpan is a sequential pattern mining algorithm described in
0107 [Pei et al., Mining Sequential Patterns by Pattern-Growth: The
0108 PrefixSpan Approach](https://doi.org/10.1109%2FTKDE.2004.77). We refer
0109 the reader to the referenced paper for formalizing the sequential
0110 pattern mining problem.
0111
0112 `spark.ml`'s PrefixSpan implementation takes the following parameters:
0113
0114 * `minSupport`: the minimum support required to be considered a frequent
0115 sequential pattern.
0116 * `maxPatternLength`: the maximum length of a frequent sequential
0117 pattern. Any frequent pattern exceeding this length will not be
0118 included in the results.
0119 * `maxLocalProjDBSize`: the maximum number of items allowed in a
0120 prefix-projected database before local iterative processing of the
0121 projected database begins. This parameter should be tuned with respect
0122 to the size of your executors.
0123 * `sequenceCol`: the name of the sequence column in dataset (default "sequence"), rows with
0124 nulls in this column are ignored.
0125
0126 **Examples**
0127
0128 <div class="codetabs">
0129
0130 <div data-lang="scala" markdown="1">
0131 Refer to the [Scala API docs](api/scala/org/apache/spark/ml/fpm/PrefixSpan.html) for more details.
0132
0133 {% include_example scala/org/apache/spark/examples/ml/PrefixSpanExample.scala %}
0134 </div>
0135
0136 <div data-lang="java" markdown="1">
0137 Refer to the [Java API docs](api/java/org/apache/spark/ml/fpm/PrefixSpan.html) for more details.
0138
0139 {% include_example java/org/apache/spark/examples/ml/JavaPrefixSpanExample.java %}
0140 </div>
0141
0142 <div data-lang="python" markdown="1">
0143 Refer to the [Python API docs](api/python/pyspark.ml.html#pyspark.ml.fpm.PrefixSpan) for more details.
0144
0145 {% include_example python/ml/prefixspan_example.py %}
0146 </div>
0147
0148 <div data-lang="r" markdown="1">
0149
0150 Refer to the [R API docs](api/R/spark.prefixSpan.html) for more details.
0151
0152 {% include_example r/ml/prefixSpan.R %}
0153 </div>
0154
0155 </div>