Spark MLlib FPGrowth关联规则算法
一.简介
FPGrowth算法是关联分析算法,它采取如下分治策略:将提供频繁项集的数据库压缩到一棵频繁模式树(FP-tree),但仍保留项集关联信息。在算法中使用了一种称为频繁模式树(Frequent Pattern Tree)的数据结构。FP-tree是一种特殊的前缀树,由频繁项头表和项前缀树构成。
二.测试数据
r z h k p
z y x w v u t s
s x o n r
x z y m t s q e
z
x z y r q t p
三.代码实现
package big.data.analyse.mllib import org.apache.log4j.{Level, Logger} import org.apache.spark.mllib.fpm.FPGrowth import org.apache.spark.{SparkContext, SparkConf} /** * 关联规则 * Created by zhen on 2019/4/11. */ object FPG { Logger.getLogger("org").setLevel(Level.WARN) def main(args: Array[String]) { val conf = new SparkConf() conf.setAppName("fpg") conf.setMaster("local[2]") val sc = new SparkContext(conf) /** * 加载数据 */ val data = sc.textFile("data/mllib/sample_fpgrowth.txt") val data_spl = data.map(row => row.split(" ")).cache() /** * 创建模型 */ val minSupport = 0.2 val numPartition = 10 val model = new FPGrowth() .setMinSupport(minSupport) .setNumPartitions(numPartition) .run(data_spl) /** * 打印结果 */ println("Number of frequent itemsets : " + model.freqItemsets.count()) model.freqItemsets.collect.foreach{itemset => println(itemset.items.mkString("[", ",", "]") + " ==> " + itemset.freq) } } }
四.结果
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。五.备注
集群模式出现以下异常【local模式无异常】;
can not set final scala.collection.mutable.ListBuffer field org.apache.spark.mllib.fpm.FPTree$Summary.nodes to scala.collection.mutable.ArrayBuffer
解决方案:
配置:conf.set("spark.serializer", "org.apache.spark.serializer.JavaSerializer")

更多精彩