21CTO社区导读:
今天我们来讨论关于一个非常有意义的话题,这就是推荐系统。我们讨论如何使用Python来构建推荐系统,我们将焦点和一些详细深度着重在如何让推荐系统开始工作。
电影推荐引擎
商品推荐引擎
机器学习推荐引擎
使用商品推荐算法的个性化商品推荐引擎
预测引擎
音乐推荐引擎
喜欢/不喜欢
赞/贬
评论
分享等
In the newer, narrower sense, collaborative filtering is a method of making automatic predictions (filtering) about the interests of a user by collecting preferences or taste information from many users (collaborating).
在较新的狭义定义下,协同过滤通过收集来自大量用户的偏好和品味信息(协作)来对用户兴趣进行自动预测(过滤)的方法。
UserRatings={
'Lisa Rose':{
'Catch Me If You Can':3.0,
'Snakes on a Plane':3.5,
'Superman Returns':3.5,
'You, Me and Dupree':2.5,
'The Night Listener':3.0,
'Snitch':3.0
},
'Gene Seymour':{
'Lady in the Water':3.0,
'Snakes on a Plane':3.5,
'Just My Luck':1.5,
'The Night Listener':3.0,
'You, Me and Dupree':3.5
},
'Michael Phillips':{
'Catch Me If You Can':2.5,
'Lady in the Water':2.5,
'Superman Returns':3.5,
'The Night Listener':4.0,
'Snitch':2.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,
'You, Me and Dupree':2.0
},
'Jack Matthews':{
'Catch Me If You Can':4.5,
'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,
'Snitch':4.5
},
'Toby':{
'Snakes on a Plane':4.5,
'Snitch':5.0
},
'Michelle Nichols':{
'Just My Luck':1.0,
'The Night Listener':4.5,
'You, Me and Dupree':3.5,
'Catch Me If You Can':2.5,
'Snakes on a Plane':3.0
},
'Gary Coleman':{
'Lady in the Water':1.0,
'Catch Me If You Can':1.5,
'Superman Returns':1.5,
'You, Me and Dupree':2.0
},
'Larry':{
'Lady in the Water':3.0,
'Just My Luck':3.5,
'Snitch':1.5,
'The Night Listener':3.5
}
}
'Lisa Rose':{
'Catch Me If You Can':3.0,
'Snakes on a Plane':3.5,
'Superman Returns':3.5,
'You, Me and Dupree':2.5,
'The Night Listener':3.0,
'Snitch':3.0
}
'Lisa Rose':{
'[b]Catch Me If You Can[/b]':3.0,
'[b]Snakes on a Plane[/b]':3.5,
'Superman Returns':3.5,
'[b]You, Me and Dupree[/b]':2.5,
'The Night Listener':3.0,
'Snitch':3.0
}
'Michelle Nichols':{两个相关电影数据:You,Me and Dupree,Catch Me if You Can与Snakes on a Plane。没有评分的有:Just My Luck,Superen Returns,Snitch。
'Just My Luck':1.0,
'The Night Listener':4.5,
'[b]You, Me and Dupree[/b]':3.5,
'[b]Catch Me If You Can[/b]':2.5,
'[b]Snakes on a Plane[/b]':3.0
}
You, Me and Dupree':{需要进行转换,我们需要定义自己的函数,命名为transform()。
'Lisa Rose':3.5,
'Michelle Nichols':3.5,
}
MovieRates={} #Declaring empty set for our new transformed data该程序执行后的返回结果如下:
def transform(): #Transformation Set
for person in UserRatings:
for movie in User[person]:
if movie not in MovieRates:
MovieRates[movie]={}
MovieRates[movie][person]=UserRatings[person][movie]
{
'The Night Listener':{
'Michelle Nichols':4.5,
'Jack Matthews':3.0,
'Lisa Rose':3.0,
'Michael Phillips':4.0,
'Gene Seymour':3.0,
'Larry':3.5,
'Claudia Puig':4.5
},
'Snitch':{
'Toby':5.0,
'Larry':1.5,
'Jack Matthews':4.5,
'Lisa Rose':3.0,
'Michael Phillips':2.0
},
'Superman Returns':{
'Jack Matthews':5.0,
'Lisa Rose':3.5,
'Michael Phillips':3.5,
'Mick LaSalle':3.0,
'Gary Coleman':1.5,
'Claudia Puig':4.0
},
'Just My Luck':{
'Michelle Nichols':1.0,
'Gene Seymour':1.5,
'Claudia Puig':3.0,
'Mick LaSalle':2.0,
'Larry':3.5
},
'You, Me and Dupree':{
'Michelle Nichols':3.5,
'Jack Matthews':3.5,
'Lisa Rose':2.5,
'Mick LaSalle':2.0,
'Gene Seymour':3.5,
'Gary Coleman':2.0,
'Claudia Puig':2.5
},
'Snakes on a Plane':{
'Toby':4.5,
'Michelle Nichols':3.0,
'Jack Matthews':4.0,
'Lisa Rose':3.5,
'Gene Seymour':3.5,
'Mick LaSalle':4.0,
'Claudia Puig':3.5
},
'Catch Me If You Can':{
'Michelle Nichols':2.5,
'Michael Phillips':2.5,
'Jack Matthews':4.5,
'Lisa Rose':3.0,
'Gary Coleman':1.5
},
'Lady in the Water':{
'Mick LaSalle':3.0,
'Jack Matthews':3.0,
'Larry':3.0,
'Gene Seymour':3.0,
'Michael Phillips':2.5,
'Gary Coleman':1.0
}
}
def cos_similarity(people,movie1,movie2):
si={}
for item in people[movie1]:
if item in people[movie2]:
si[item]=1
if len(si)==0:
return 0
sum1=0
sum21=0
sum22=0
for item in si:
sum1+=(people[movie1][item]*people[movie2][item])
sum21+=pow(people[movie1][item],2)
sum22+=pow(people[movie2][item],2)
if sum21==0 or sum22==0:
return 0
return round(sum1/(sqrt(sum21)*sqrt(sum22)),2)
movies_watched=["You, Me and Dupree","Catch Me If You Can","Snitch"]
现在系统经过了学习,会为我们推荐喜欢的电影。当前是以前的计算结果 ,也会做出输出。------------------------------
| You, Me and Dupree |
-------------------------------
Catch Me If You Can 0.97
Just My Luck 0.85
Lady in the Water 0.96
Snakes on a Plane 0.97
Snitch 1.0
Superman Returns 0.98
The Night Listener 0.96
------------------------------
| Catch Me If You Can |
------------------------------
Just My Luck 1.0
Lady in the Water 0.98
Snakes on a Plane 0.99
Snitch 1.0
Superman Returns 1.0
The Night Listener 0.92
You, Me and Dupree 0.97
------------------------------
| Snitch |
------------------------------
Catch Me If You Can 1.0
Just My Luck 1.0
Lady in the Water 0.91
Snakes on a Plane 0.99
Superman Returns 0.99
The Night Listener 0.88
You, Me and Dupree 1.0
------------------------------
------------------------------
| You, Me and Dupree |
-------------------------------
Snitch 1.0
Superman Returns 0.98
------------------------------
| Catch Me If You Can |
------------------------------
Just My Luck 1.0
Lady in the Water 0.98
Snakes on a Plane 0.99
Snitch 1.0
Superman Returns 1.0
------------------------------
| Snitch |
------------------------------
Catch Me If You Can 1.0
Just My Luck 1.0
Snakes on a Plane 0.99
Superman Returns 0.99
You, Me and Dupree 1.0
------------------------------
作者:Mitko
编译:21CTO社区
来源:scienceEz.com
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。