FP-Growth
import org.apache.spark.ml.fpm.FPGrowth
val dataset = spark.createDataset(Seq(
"1 2 5",
"1 2 3 5",
"1 2")
).map(t => t.split(" ")).toDF("items")
val fpgrowth = new FPGrowth().setItemsCol("items").setMinSupport(0.5).setMinConfidence(0.6)
val model = fpgrowth.fit(dataset)
// Display frequent itemsets.
model.freqItemsets.show()
// Display generated association rules.
model.associationRules.show()
// transform examines the input items against all the association rules and summarize the
// consequents as prediction
model.transform(dataset).show()
/*
Output:
+---------+----+
| items|freq|
+---------+----+
| [1]| 3|
| [2]| 3|
| [2, 1]| 3|
| [5]| 2|
| [5, 2]| 2|
|[5, 2, 1]| 2|
| [5, 1]| 2|
+---------+----+
+----------+----------+------------------+----+
|antecedent|consequent| confidence|lift|
+----------+----------+------------------+----+
| [2, 1]| [5]|0.6666666666666666| 1.0|
| [5, 1]| [2]| 1.0| 1.0|
| [2]| [1]| 1.0| 1.0|
| [2]| [5]|0.6666666666666666| 1.0|
| [5]| [2]| 1.0| 1.0|
| [5]| [1]| 1.0| 1.0|
| [1]| [2]| 1.0| 1.0|
| [1]| [5]|0.6666666666666666| 1.0|
| [5, 2]| [1]| 1.0| 1.0|
+----------+----------+------------------+----+
+------------+----------+
| items|prediction|
+------------+----------+
| [1, 2, 5]| []|
|[1, 2, 3, 5]| []|
| [1, 2]| [5]|
+------------+----------+
*/Last updated