Python机器学习案例——基于用户的电影推荐系统

在这里插入图片描述

Python机器学习案例:基于用户的电影推荐系统

01 案例背景

本案例实现电影推荐系统。该系统的工作过程大致可以简化为以下三个步骤。

(1)寻找与特定用户最相近的N个其他用户;

(2)找出这N个相似用户的观影列表;

(3)从相似用户的观影列表中找出特定用户未观看过的电影,将这些电影按评分由高到低输出。

02 实现代码

Python实现代码如下所示:
from math import sqrt,pow
import operator
class UserCf():

#获得初始化数据
def init(self,data):
self.data=data;

#通过用户名获得电影列表,仅调试使用
def getItems(self,username1,username2):
return self.data[username1],self.data[username2]

#计算两个用户的皮尔逊相关系数
def pearson(self,user1,user2):#数据格式为:电影,评分 {‘Snakes on a Plane’: 4.5, ‘You, Me and Dupree’: 1.0, ‘Superman Returns’: 4.0}
sumXY=0.0;
n=0;
sumX=0.0;
sumY=0.0;
sumX2=0.0;
sumY2=0.0;
try:
for movie1,score1 in user1.items():
if movie1 in user2.keys():#计算公共的电影的评分
n+=1;
sumXY+=score1*user2[movie1]
sumX+=score1;
sumY+=user2[movie1]
sumX2+=pow(score1,2)
sumY2+=pow(user2[movie1],2)

molecule=sumXY-(sumXsumY)/n;
denominator=sqrt((sumX2-pow(sumX,2)/n)
(sumY2-pow(sumY,2)/n))
r=molecule/denominator
except Exception as e:
print(“异常信息:”,e.message)
return r

#计算与当前用户的距离,获得最临近的用户
def nearstUser(self,username,n=1):
distances={};#用户,相似度
for otherUser,items in self.data.items():#遍历整个数据集
if otherUser not in username:#非当前的用户
distance=self.pearson(self.data[username],self.data[otherUser])#计算两个用户的相似度
distances[otherUser]=distance
sortedDistance=sorted(distances.items(),key=operator.itemgetter(1),reverse=True);#最相似的N个用户
return sortedDistance[:n]

def recomand(self,username,n=1):
recommand={};#待推荐的电影
for user,score in dict(self.nearstUser(username,n)).items():#最相近的n个用户
print(“推荐的用户:”,(user,score))
for movies,scores in self.data[user].items():#推荐的用户的电影列表
if movies not in self.data[username].keys():#当前username没有看过
print(“%s为该用户推荐的电影:%s”%(user,movies))
if movies not in recommand.keys():#添加到推荐列表中
recommand[movies]=scores

return sorted(recommand.items(),key=operator.itemgetter(1),reverse=True);#对推荐的结果按照电影评分排序

if name==‘main’:
users = {‘Lisa Rose’: {‘Lady in the Water’: 2.5, ‘Snakes on a Plane’: 3.5,
‘Just My Luck’: 3.0, ‘Superman Returns’: 3.5, ‘You, Me and Dupree’: 2.5,
‘The Night Listener’: 3.0},

‘Gene Seymour’: {‘Lady in the Water’: 3.0, ‘Snakes on a Plane’: 3.5,
‘Just My Luck’: 1.5, ‘Superman Returns’: 5.0, ‘The Night Listener’: 3.0,
‘You, Me and Dupree’: 3.5},

‘Michael Phillips’: {‘Lady in the Water’: 2.5, ‘Snakes on a Plane’: 3.0,
‘Superman Returns’: 3.5, ‘The Night Listener’: 4.0},

‘Claudia Puig’: {‘Snakes on a Plane’: 3.5, ‘Just My Luck’: 3.0,
‘The Night Listener’: 4.5, ‘Superman Returns’: 4.0,
‘You, Me and Dupree’: 2.5},

‘Mick LaSalle’: {‘Lady in the Water’: 3.0, ‘Snakes on a Plane’: 4.0,
‘Just My Luck’: 2.0, ‘Superman Returns’: 3.0, ‘The Night Listener’: 3.0,
‘You, Me and Dupree’: 2.0},

‘Jack Matthews’: {‘Lady in the Water’: 3.0, ‘Snakes on a Plane’: 4.0,
‘The Night Listener’: 3.0, ‘Superman Returns’: 5.0, ‘You, Me and Dupree’: 3.5},

‘Toby’: {‘Snakes on a Plane’: 4.5, ‘You, Me and Dupree’: 1.0, ‘Superman Returns’: 4.0}
}

userCf=UserCf(data=users)
recommandList=userCf.recomand(‘Toby’, 2)
9print(“最终推荐:%s”%recommandList)

03 运行结果

运行以上代码的输出结果如下所示。

排序后的用户为:[('Lisa Rose', 0.9912407071619299), ('Mick LaSalle', 0.9244734516419049), ('Claudia Puig', 0.8934051474415647), ('Jack Matthews', 0.66284898035987), ('Gene Seymour', 0.38124642583151164), ('Michael Phillips', -1.0)]
推荐的用户:('Lisa Rose', 0.9912407071619299)
Lisa Rose为该用户推荐的电影:Lady in the Water
Lisa Rose为该用户推荐的电影:Just My Luck
Lisa Rose为该用户推荐的电影:The Night Listener
推荐的用户:('Mick LaSalle', 0.9244734516419049)
Mick LaSalle为该用户推荐的电影:Lady in the Water
Mick LaSalle为该用户推荐的电影:Just My Luck
Mick LaSalle为该用户推荐的电影:The Night Listener
最终推荐:[('Just My Luck', 3.0), ('The Night Listener', 3.0), ('Lady in the Water', 2.5)]

到这里一个简单的机器学习案例就结束啦!小伙伴们快学起来!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值