专栏名称: 大数据挖掘DT数据分析
实战数据资源提供。数据实力派社区,手把手带你玩各种数据分析,涵盖数据分析工具使用,数据挖掘算法原理与案例,机器学习,R语言,Python编程,爬虫。如需发布广告请联系: hai299014
目录
相关文章推荐
天池大数据科研平台  ·  DeepSeek开源放大招:FlashMLA ... ·  昨天  
数据派THU  ·  李飞飞巴黎演讲:如果 AI ... ·  4 天前  
数据派THU  ·  EvalPlanner:基于“计划-执行”双 ... ·  3 天前  
数据派THU  ·  【ICLR2025】VEVO:基于自监督解耦 ... ·  4 天前  
51好读  ›  专栏  ›  大数据挖掘DT数据分析

[python]评论文本挖掘:找出兴趣相投的用户并作产品推荐

大数据挖掘DT数据分析  · 公众号  · 大数据  · 2017-04-10 23:03

正文



数据挖掘入门与实战  公众号: datadw



目录

  1. 过程

  2. 电影评价多重分类

  3. 用户信息录入

  4. 计算皮尔逊相关系数找出兴趣相投的用户插入自己的数据

  5. 向某用户推荐电影加权平均所有人的评价

  6. 结果与分析


过程:

用爬虫抓取豆瓣电影用户信息
用多重分类法,定义电影评价等级
计算自己与用户的皮尔逊相关度
以人为主体分析相似度:找出志同道合的人,可以发现潜在喜欢的商品
以商品为主体分析相似度:找出相似的商品,可以发现潜在的客户(如亚马逊的‘买了商品A的用户还买了商品B’)


电影评价多重分类:

  1. 很差

  2. 较差

  3. 还行

  4. 推荐

  5. 力荐


  1. #-*- coding: utf-8 -*-

  2. import json

  3. import sys

  4. reload(sys)

  5. sys.setdefaultencoding( "utf-8" )

  6. user_info = {}

  7. #爬取到的数据

  8. user_dict = {

  9. 'ns2250225' :[ 4 , 3 , 4 , 5 , 4 ],

  10. 'justin' :[ 3 , 4 , 3 , 4 , 2 ],

  11. 'totox' :[ 2 , 3 , 5 , 1 , 4 ],

  12. 'fabrice' :[ 4 , 1 , 3 , 4 , 5 ],

  13. 'doreen' :[ 3 , 4 , 2 , 5 , 3 ]

  14. }

  15. #录入用户数据

  16. def user_data(user_dict):

  17. for name in user_dict:

  18. user_info[name] = {u '消失的爱人' : user_dict[name][ 0 ]}

  19. user_info[name][u '霍比特人3' ] = user_dict[name][ 1 ]

  20. user_info[name][u '神去村' ] = user_dict[name][ 2 ]

  21. user_info[name][u '泰坦尼克号' ] = user_dict[name][ 3 ]

  22. user_info[name][u '这个杀手不太冷' ] = user_dict[name][ 4 ]

  23. user_data(user_dict)

  24. #存放用户数据

  25. try :

  26. with open( 'user_data.txt' , 'w' ) as data:

  27. for key in user_info:

  28. data.write(key)

  29. for key2 in user_info[key]:

  30. data.write( '\t' )

  31. data.write(key2)

  32. data.write( '\t' )

  33. data.write( '\t' )

  34. data.write(str(user_info[key][key2]))

  35. data.write( '\n' )

  36. data.write( '\n' )

  37. except IOError as err:

  38. print ( 'File error: ' + str(err))




计算皮尔逊相关系数,找出兴趣相投的用户:(插入自己的数据)


  1. from math import sqrt

  2. #计算皮尔逊相关度(1为完全正相关,-1为完成负相关)

  3. def sim_pearson(prefs, p1, p2):

  4. # Get the list of mutually rated items

  5. si = {}

  6. for item in prefs[p1]:

  7. if item in prefs[p2]:

  8. si[item] = 1

  9. # if they are no ratings in common, return 0

  10. if len(si) == 0 :

  11. return 0

  12. # Sum calculations

  13. n = len(si)

  14. # Sums of all the preferences

  15. sum1 = sum([prefs[p1][it] for it in si])

  16. sum2 = sum([prefs[p2][it] for it in si])

  17. # Sums of the squares

  18. sum1Sq = sum([pow(prefs[p1][it], 2 ) for it in si])

  19. sum2Sq = sum([pow(prefs[p2][it], 2 ) for it in si])

  20. # Sum of the products

  21. pSum = sum([prefs[p1][it] * prefs[p2][it] for it in si])

  22. # Calculate r (Pearson score)

  23. num = pSum - (sum1 * sum2 / n)

  24. den = sqrt((sum1Sq - pow(sum1, 2 ) / n) * (sum2Sq - pow(sum2, 2 ) / n))

  25. if den == 0 :

  26. return 0

  27. r = num / den

  28. return r

  29. #插入自己的数据

  30. user_info[ 'me' ] = {u '消失的爱人' : 5 ,

  31. u '神去村' : 3 ,

  32. u '炸裂鼓手' : 5 }

  33. #找出皮尔逊相关系数>0的用户,说明该用户与自己的电影品味比较相近

  34. for user in user_info:

  35. res = sim_pearson(user_info, 'me' , user)

  36. if res > 0 :

  37. print ( 'the user like %s is : %s' % ( 'me' , user))

  38. print ( 'result :%f\n' % res)




向某用户推荐电影(加权平均所有人的评价)

  1. #向某个用户推荐电影(加权平均所有人的评价值)

  2. def getRecommendations(prefs,person,similarity=sim_pearson):

  3. totals={}

  4. simSums={}

  5. for other in prefs:

  6. # don't compare me to myself

  7. if other==person: continue

  8. sim=similarity(prefs,person,other)

  9. # ignore scores of zero or lower

  10. if sim<= 0 : continue

  11. for item in prefs[other]:

  12. # only score movies I haven't seen yet

  13. if item not in prefs[person] or prefs[person][item]== 0 :

  14. # Similarity * Score

  15. totals.setdefault(item, 0 )

  16. totals[item]+=prefs[other][item]*sim

  17. # Sum of similarities

  18. simSums.setdefault(item, 0 )

  19. simSums[item]+=sim

  20. # Create the normalized list

  21. rankings=[(total/simSums[item],item) for item,total in totals.items()]

  22. # Return the sorted list

  23. rankings.sort()

  24. rankings.reverse()

  25. return rankings

  26. #向我推荐电影

  27. res = getRecommendations(user_info, "me" )

  28. print ( 'Recommand watching the movie:' )

  29. print json.dumps(res, encoding= 'UTF-8' , ensure_ascii= False )


结果与分析:




数据挖掘入门与实战

搜索添加微信公众号:datadw







请到「今天看啥」查看全文