FP-Growth
The FP-growth algorithm is described in the paper Han et al., Mining frequent patterns without candidate generation, where β€œFP” stands for frequent pattern. Given a dataset of transactions, the first step of FP-growth is to calculate item frequencies and identify frequent items. Different from Apriori-like algorithms designed for the same purpose, the second step of FP-growth uses a suffix tree (FP-tree) structure to encode transactions without generating candidate sets explicitly, which are usually expensive to generate. After the second step, the frequent itemsets can be extracted from the FP-tree. In spark.mllib, we implemented a parallel version of FP-growth called PFP, as described in Li et al., PFP: Parallel FP-growth for query recommendation. PFP distributes the work of growing FP-trees based on the suffixes of transactions, and hence is more scalable than a single-machine implementation. We refer users to the papers for more details.
spark.ml's FP-growth implementation takes the following (hyper-)parameters:
minSupport: the minimum support for an itemset to be identified as frequent. For example, if an item appears 3 out of 5 transactions, it has a support of 3/5=0.6.
minConfidence: minimum confidence for generating Association Rule. Confidence is an indication of how often an association rule has been found to be true. For example, if in the transactions itemset X appears 4 times, X and Y co-occur only 2 times, the confidence for the rule X => Y is then 2/4 = 0.5. The parameter will not affect the mining for frequent itemsets, but specify the minimum confidence for generating association rules from frequent itemsets.
numPartitions: the number of partitions used to distribute the work. By default the param is not set, and number of partitions of the input dataset is used. The FPGrowthModel provides:
freqItemsets: frequent itemsets in the format of DataFrame("items"[Array], "freq"[Long]) associationRules: association rules generated with confidence above minConfidence, in the format of DataFrame("antecedent"[Array], "consequent"[Array], "confidence"[Double]).
transform: For each transaction in itemsCol, the transform method will compare its items against the antecedents of each association rule. If the record contains all the antecedents of a specific association rule, the rule will be considered as applicable and its consequents will be added to the prediction result. The transform method will summarize the consequents from all the applicable rules as prediction. The prediction column has the same data type as itemsCol and does not contain existing items in the itemsCol.
1
import org.apache.spark.ml.fpm.FPGrowth
2
​
3
val dataset = spark.createDataset(Seq(
4
"1 2 5",
5
"1 2 3 5",
6
"1 2")
7
).map(t => t.split(" ")).toDF("items")
8
​
9
val fpgrowth = new FPGrowth().setItemsCol("items").setMinSupport(0.5).setMinConfidence(0.6)
10
val model = fpgrowth.fit(dataset)
11
​
12
// Display frequent itemsets.
13
model.freqItemsets.show()
14
​
15
// Display generated association rules.
16
model.associationRules.show()
17
​
18
// transform examines the input items against all the association rules and summarize the
19
// consequents as prediction
20
model.transform(dataset).show()
21
/*
22
Output:
23
+---------+----+
24
| items|freq|
25
+---------+----+
26
| [1]| 3|
27
| [2]| 3|
28
| [2, 1]| 3|
29
| [5]| 2|
30
| [5, 2]| 2|
31
|[5, 2, 1]| 2|
32
| [5, 1]| 2|
33
+---------+----+
34
​
35
+----------+----------+------------------+----+
36
|antecedent|consequent| confidence|lift|
37
+----------+----------+------------------+----+
38
| [2, 1]| [5]|0.6666666666666666| 1.0|
39
| [5, 1]| [2]| 1.0| 1.0|
40
| [2]| [1]| 1.0| 1.0|
41
| [2]| [5]|0.6666666666666666| 1.0|
42
| [5]| [2]| 1.0| 1.0|
43
| [5]| [1]| 1.0| 1.0|
44
| [1]| [2]| 1.0| 1.0|
45
| [1]| [5]|0.6666666666666666| 1.0|
46
| [5, 2]| [1]| 1.0| 1.0|
47
+----------+----------+------------------+----+
48
​
49
+------------+----------+
50
| items|prediction|
51
+------------+----------+
52
| [1, 2, 5]| []|
53
|[1, 2, 3, 5]| []|
54
| [1, 2]| [5]|
55
+------------+----------+
56
​
57
​
58
​
59
*/
Copied!
Last modified 1yr ago
Copy link