spark mllib 关联规则

1 maven依赖包如下

<dependency>
    <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.11</artifactId>
        <version>1.5.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-mllib_2.11</artifactId>
        <version>1.5.1</version>
</dependency>

2 计算数据的格式

a b c
a b
a b c d
a c d

3 关联规则代码如下

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.log4j.Logger
import org.apache.log4j.Level
import org.apache.spark.mllib.fpm.FPGrowth

object fpg {
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("关联规则").setMaster("local[2]")
    val sc = new SparkContext(conf)
    Logger.getRootLogger.setLevel(Level.WARN)
    val data = sc.textFile("f:/data/item.txt", 10)
    val example = data.map(_.split(" ")).cache()

    val minSupport = 0.5
    val numPartitions = 10
    val model = new FPGrowth().setMinSupport(minSupport).setNumPartitions(numPartitions).run(example)
    println("出现频次----------------")
    model.freqItemsets.collect().foreach { x =>
      println(x.items.mkString("[", ",", "],") + x.items.length)
    }
    println("置信度---------------")
    model.generateAssociationRules(minSupport).collect().foreach { x =>
      println(x.antecedent.mkString("[", ",", "]") + " => " + x.consequent.mkString("[", ",", "]") + ", " + x.confidence)
    }

  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值