关联推荐实战

2021-04-21  本文已影响0人  郭彦超

关联推荐并不是所有人都有了解,但大家熟知的啤酒和尿不湿的故事应该都有听说,该案例就是亚马逊通过关联规则实现的,算法的主要原理是以购物车为单位,分析商品频繁出现的概率,即一件商品出现在购物车后会再加入另一件商品的概率

介绍

关联推荐这里主要使用FP-growth,该方法比传统的Apriori算法性能要好很多,关于FP树的实现这里不做解释,感兴趣的小朋友可以百度了解下;下面直接上代码

spark实现:

import org.apache.spark.ml.fpm.FPGrowth
val df = sql("select u_i ,collect_set(pid) items from tab group by u_i")
val fpgrowth = new FPGrowth().setItemsCol("items").setMinSupport(0.0001).setMinConfidence(0.01)
val model = fpgrowth.fit(df)
  
model.associationRules.createOrReplaceTempView("ar")
select title , recommend , confidence, lift from(
select title as recommend , a.id, confidence, lift from (
select explode(antecedent ) rid, consequent[0]  id, lift, confidence from ar where  lift>2 and confidence>0.1
) as a left join app.product_model as b on a.rid=b.id ) c left join app.product_model as d where c.id=d.id order by d.id,lift desc

查看效果

上一篇 下一篇

猜你喜欢

热点阅读