数据挖掘干货总结(五)--推荐算法之CB

本文共计927字,预计阅读时长六分钟



推荐算法(一)--CB算法

 

推荐的本质

推荐分为非个性化和个性化非个性化推荐比如各类榜单而本系列主要介绍个性化推荐

在合适的场景合适的时机通过合适的渠道把合适的内容推荐给合适的用户

 

推荐算法的种类

1. 基于内容Content Based

2. 基于协同Collaboration Filtering

– User Based CF

– Item Based CF

 

、CB算法详解

1. Content的内容

① 只引入Item属性的Content Based推荐

优点

a.提升推荐结果的相关性

b结果可解释

c.推荐结果容易被用户感知

缺点

a.无个性化

b.依赖于对item的深入分析

 

② 引入ItemUser属性的Content Based推荐

在上一步的item提取特征之后的基础上加上了用户的一些行为数据。需要注意行为数据也需要特征化后才能和之前的item特征放在一起进行分析其中可能会涉及到一些one-hot编码的问题这里不展开需要了解的话见:

怎么理解one-hot?

 

优点

a.用户模型刻画了用户兴趣需求

b.推荐形式多样具有个性化

c.结果可解释

缺点

a.推荐精度低

b.马太效应

c.用户行为稀疏导致覆盖率低


2. 需求举例(仅基于item的特征):

用户小明听了一首歌每首歌(item)的数据属性包括编号(id)和内容(content),

id content

id1 王铮亮 时间都去哪了 《私人订制插曲

... ...

根据CB,向他推荐10首歌

 

3. 解决步骤和核心代码:

① 对歌曲库中的每首歌content进行分词并用tf_idf赋值

+++jieba_score_udf.py+++

# coding=utf-8

import  sys

reload(sys)

sys.setdefaultencoding('utf-8')

import jieba.analyse

import jieba

import json

import re

 

#正则匹配中文

pattern = re.compile(u'[\u4e00-\u9fa5]')

 

#切词并用tf_idf赋值

for line in sys.stdin:

   cols = line.strip().split('\t')

   id = cols[0]

   words_list = jieba.analyse.extract_tags(cols[1],topK=10,withWeight=True)

for (word,score) in words_list:

if len(re.findall(pattern,word)) != 0:

print '%s\t%s\t%s' % (id,word,score)

 

大致得到数据是随便编的) t1

id word score  

id1 时间 0.1

id1 王铮亮 0.5

id1 私人 0.5

id2 时间 0.4

id2 0.4

id2 0.1

id3 私人 0.3

... ... ...

 

② 在hive中调用这个udf,得到倒排表

hive> add 'udfpath/jieba_score_udf.py'

hive> select

hive> word,collect_list(concat_ws(':',id,score)) as id_score_list

hive> from

hive> (select transform(id,desc) using 'python jieba_score_udf.py' as(id,word,score)

hive> from badou.musics sort by score desc

hive> ) t

hive> group by word;

 

大致得到数据是随便编的) t2

word id_score_list

王铮亮 [id1:0.5,...]

时间 [id2:0.4, id1:0.1...]

私人 [id1:0.5, id3:0.3,...]

... ...

 

③ 得到ids推荐列表

hive> select tt1.id,collect_list(concat_ws('_', tt2.id, nscore)) ids

hive> from

hive> (select tt1.id, tt2.id, sum(tt2.score) nscore

hive> from

hive>   (select tt1.id, tt2.id, tt2.score new_score

hive>     from

hive>       (select id, word from t1) tt1

hive>     join

hive>       (select word, id, score from t1) tt2

hive>     on tt1.word = tt2.word

hive>   ) tt3

hive> where tt1.id <> tt2.id

hive> group by tt1.id,tt2.id

hive> order by nscore desc) tt4;

 

大致得到数据是随便编的) t3

id ids

id1 [id2:0.5,...]

id2 [id1:0.5, id3:0.1...]

id3 [id2:0.5, id1:0.3,...]

... ...

 

④ 推荐

i) 如果小明听的这首歌idt3表中存在那么直接取出对应的ids即可

ii) 如果小明听的这首歌idt3表中不存在

a. 需要在线对其content做分词得到一个word列表:(word1, word2, word3,...)

b. t1中找出对应的id分数求和排序top10即可

hive> select id, sum(score) nscore

hive> from

hive> (select id, word, score from t1

hive> where word in (word1, word2, word3,...) tt

hive> group by id

hive> order by nscore desc

hive> limit  10;

 

或者在t2倒排表中选取对应的ids,解析后求和排序结果都一样,但可能会更快一点

hive> select

hive> split(id_score,':')[0] as id,

hive> sum(split(id_score,':')[1] as score) nscore

hive> from

hive>   (select word, explode(id_score_list,',') id_score

hive>   from t2

hive>   where word in (word1, word2, word3,...)

hive> ) ttt

hive> group by id

hive> order by nscore desc

hive> limit  10; 


以上.


ps:

CF的推荐算法点下方链接

(担心有人会太长不看)


推荐算法之CF




每天向你推送最实用的干货,记得收藏点赞置顶哦~

不切实际的期望和恐惧是造成焦虑的根源

我们需要以实际情况和发生概率为依据

避免非黑即白地去看待事物