我有以下数据集:
[A,D]
[C,A,B]
[A]
[A,E,D]
[B,D]
我正在尝试使用Spark Mllib使用Frequent Pattern Mining提取一些关联规则。为此,我有以下代码:
val transactions = sc.textFile("/user/cloudera/teste")
import org.apache.spark.mllib.fpm.AssociationRules
import org.apache.spark.mllib.fpm.FPGrowth.FreqItemset
val freqItemsets = transactions.repartition(10).map(_.split(",")).flatMap(xs =>
(xs.combinations(1) ++ xs.combinations(2) ++ xs.combinations(3) ++ xs.combinations(4) ++ xs.combinations(5)).filter(_.nonEmpty).map(x => (x.toList, 1L)) ).reduceByKey(_ + _).map{case (xs, cnt) => new FreqItemset(xs.toArray, cnt)}
val ar = new AssociationRules().setMinConfidence(0.8)
val results = ar.run(freqItemsets)
results.collect().foreach { rule =>
println("[" + rule.antecedent.mkString(",")
+ "=>"
+ rule.consequent.mkString(",") + "]," + rule.confidence)}
但所有提取的规则都有等于1的置信度:
[[C=>A],1.0
[[C=>B]],1.0
[A,B]=>[C],1.0
[E=>D]],1.0
[E=>[A],1.0
[A=>B]],1.0
[A=>[C],1.0
[[C,A=>B]],1.0
[[A=>D]],1.0
[E,D]=>[A],1.0
[[A,E=>D]],1.0
[[C,B]=>A],1.0
[[B=>D]],1.0
[B]=>A],1.0
[B]=>[C],1.0
我真的不明白我的代码中存在的问题......任何人都知道我有什么错误来计算信心?
非常感谢!
答案 0 :(得分:0)
您的数据集太小了。数据中任何项目的最大频率为3.因此您可以有信心0,1 / 3,1 / 2,2 / 3,1。只有1大于0.8。
尝试将最低置信度设置为0.6,然后实际可以获得
[A]=>[D] confidence 0.666