(三)协同过滤算法之基于物品的推荐算法python实现
一、背景介紹
互聯(lián)網(wǎng)的迅猛發(fā)展將人類帶入了信息社會和網(wǎng)絡(luò)經(jīng)濟(jì)時代,信息化影響到了生活的方方面面。但是隨著互聯(lián)網(wǎng)產(chǎn)業(yè)的擴(kuò)大,為用戶提供更多選的同時也帶來了篩選與推薦的難題。于是便提出了推薦算法幫助用戶快速找到自己喜愛的東西。例如京東、淘寶、美團(tuán)等,在用戶購買物品后,均會給用戶推薦他們可能喜歡的物品,不僅免去了用戶不斷查找類似物品的煩惱,而且也使得用戶可以貨比多家,最終找到自己物美價廉的商品,而相關(guān)的網(wǎng)站平臺也可以提升自己的銷量。電影推薦也是比較常見的,例如用戶觀看了阿甘正傳,可能推薦給用戶肖申克的救贖、當(dāng)幸福來敲門等,推薦相關(guān)的應(yīng)用數(shù)不勝數(shù),但其核心就是相關(guān)的推薦算法的組合。
二、推薦算法相關(guān)介紹
目前有關(guān)個性化推薦算法主要分為三大類:1.基于協(xié)同過濾的推薦;2.基于內(nèi)容過濾的推薦和3.社會化推薦。
本文主要討論基于協(xié)同過濾的推薦,而該算法也可以劃分為兩類:
1.基于用戶的協(xié)同過濾算法(UserCF)
該算法利用用戶之間的相似性來推薦用戶感興趣的信息,個人通過合作的機(jī)制給予信息相當(dāng)程度的回應(yīng)(如評分)并記錄下來以達(dá)到過濾的目的進(jìn)而幫助別人篩選信息,回應(yīng)不一定局限于特別感興趣的,特別不感興趣信息的紀(jì)錄也相當(dāng)重要。但有很難解決的兩個問題,一個是稀疏性,即在系統(tǒng)使用初期由于系統(tǒng)資源還未獲得足夠多的評價,很難利用這些評價來發(fā)現(xiàn)相似的用戶。另一個是可擴(kuò)展性,隨著系統(tǒng)用戶和資源的增多,系統(tǒng)的性能會越來越差。
2.基于物品的協(xié)同過濾算法(ItemCF)
內(nèi)容過濾根據(jù)信息資源與用戶興趣的相似性來推薦商品,通過計算用戶興趣模型和商品特征向量之間的向量相似性,主動將相似度高的商品發(fā)送給該模型的客戶。由于每個客戶都獨立操作,擁有獨立的特征向量,不需要考慮別的用戶的興趣,不存在評價級別多少的問題,能推薦新的項目或者是冷門的項目。這些優(yōu)點使得基于內(nèi)容過濾的推薦系統(tǒng)不受冷啟動和稀疏問題的影響
三、基于物品的推薦算法以及流程
例如前面背景中介紹的,用戶喜歡看阿甘正傳,且給了高評分后,那么系統(tǒng)將會尋找與阿甘正傳類似的電影推薦給用戶。
算法流程算法流程1
構(gòu)建用戶–>物品的倒排
如下表,行表示用戶,列表示物品,1表示用戶喜歡該物品
| A | 1 | 1 | 1 | ||
| B | 1 | 1 | 1 | ||
| C | 1 | 1 | |||
| D | 1 | 1 | 1 | ||
| E | 1 | 1 |
算法流程2
構(gòu)建物品與物品的同現(xiàn)矩陣
共現(xiàn)矩陣C表示同時喜歡兩個物品的用戶數(shù),是根據(jù)用戶物品倒排表計算出來的。如根據(jù)上面的用戶物品倒排表可以計算出如下的共現(xiàn)矩陣C:
| a | 1 | 2 | |||
| b | 1 | 2 | 2 | 1 | |
| c | 2 | 2 | 1 | ||
| d | 2 | 2 | 2 | ||
| e | 1 | 1 |
算法流程3
計算物品之間的相似度,即計算相似矩陣
其中兩個物品之間的相似度如何計算?
設(shè)|N(i)|表示喜歡物品i的用戶數(shù),|N(i)?N(j)|表示同時喜歡物品i,j的用戶數(shù),則物品i與物品j的相似度為:
wij=|N(i)?N(j)||N(i)|(1)(1)wij=|N(i)?N(j)||N(i)|
(1)式有一個問題,當(dāng)物品j是一個很熱門的商品時,人人都喜歡,那么wijwij就會很接近于1,即(1)式會讓很多物品都和熱門商品有一個很大的相似度,所以可以改進(jìn)一下公式:
wij=|N(i)?N(j)||N(i)||N(j)|√(2)(2)wij=|N(i)?N(j)||N(i)||N(j)|
算法流程2中的共現(xiàn)矩陣C其實就是式(2)的分子,矩陣N(用于計算分母)表示喜歡某物品的用戶數(shù)(是總的用戶數(shù)),則(2)式中的分母便很容易求解出來了。
矩陣N如下所示:
| 用戶數(shù) | 2 | 3 | 3 | 4 | 1 |
利用式(2)便能計算物品之間的余弦相似矩陣如下:
| a | 0.41 | 0.71 | |||
| b | 0.41 | 0.67 | 0.58 | 0.58 | |
| c | 0.67 | 0.58 | 0.58 | ||
| d | 0.71 | 0.58 | 0.58 | ||
| e | 0.58 | 0.58 |
算法流程4
根據(jù)用戶的歷史記錄,給用戶推薦物品;
最終推薦的是什么物品,是由預(yù)測興趣度決定的。
物品j預(yù)測興趣度=用戶喜歡的物品i的興趣度×物品i和物品j的相似度
例如:A用戶喜歡a,b,d ,興趣度分別為1,1,1
- 推薦c的預(yù)測興趣度=1X0.67+1X0.58=1.25
- 推薦e的預(yù)測興趣度=1X0.58=0.58
四、python實現(xiàn)算法
1.數(shù)據(jù)描述
該數(shù)據(jù)為用戶,興趣度,物品
#用戶,興趣度,物品uid_score_bid = ['A,1,a', 'A,1,b', 'A,1,d', 'B,1,b', 'B,1,c', 'B,1,e', 'C,1,c', 'C,1,d', 'D,1,b', 'D,1,c', 'D,1,d','E,1,a', 'E,1,d'];2.python實現(xiàn)物品推薦
#!/usr/bin/python # -*- coding: UTF-8 -*- from math import sqrt import operator#1.構(gòu)建用戶-->物品的倒排 def loadData(files):data ={};for line in files:user,score,item=line.split(",");data.setdefault(user,{});data[user][item]=score;print "----1.用戶:物品的倒排----"print datareturn data#2.計算 # 2.1 構(gòu)造物品-->物品的共現(xiàn)矩陣 # 2.2 計算物品與物品的相似矩陣 def similarity(data):# 2.1 構(gòu)造物品:物品的共現(xiàn)矩陣N={};#喜歡物品i的總?cè)藬?shù)C={};#喜歡物品i也喜歡物品j的人數(shù)for user,item in data.items():for i,score in item.items():N.setdefault(i,0);N[i]+=1;C.setdefault(i,{});for j,scores in item.items():if j not in i:C[i].setdefault(j,0);C[i][j]+=1;print "---2.構(gòu)造的共現(xiàn)矩陣---"print ('N:',N);print ('C',C);#2.2 計算物品與物品的相似矩陣W={};for i,item in C.items():W.setdefault(i,{});for j,item2 in item.items():W[i].setdefault(j,0);W[i][j]=C[i][j]/sqrt(N[i]*N[j]);print "---3.構(gòu)造的相似矩陣---"print Wreturn W#3.根據(jù)用戶的歷史記錄,給用戶推薦物品 def recommandList(data,W,user,k=3,N=10):rank={};for i,score in data[user].items():#獲得用戶user歷史記錄,如A用戶的歷史記錄為{'a': '1', 'b': '1', 'd': '1'}for j,w in sorted(W[i].items(),key=operator.itemgetter(1),reverse=True)[0:k]:#獲得與物品i相似的k個物品if j not in data[user].keys():#該相似的物品不在用戶user的記錄里rank.setdefault(j,0);rank[j]+=float(score) * w;print "---4.推薦----"print sorted(rank.items(),key=operator.itemgetter(1),reverse=True)[0:N];return sorted(rank.items(),key=operator.itemgetter(1),reverse=True)[0:N];if __name__=='__main__':#用戶,興趣度,物品uid_score_bid = ['A,1,a', 'A,1,b', 'A,1,d', 'B,1,b', 'B,1,c', 'B,1,e', 'C,1,c', 'C,1,d', 'D,1,b', 'D,1,c', 'D,1,d','E,1,a', 'E,1,d'];data=loadData(uid_score_bid);#獲得數(shù)據(jù)W=similarity(data);#計算物品相似矩陣recommandList(data,W,'A',3,10);#推薦實驗結(jié)果如下:
----1.用戶:物品的倒排---- {'A': {'a': '1', 'b': '1', 'd': '1'}, 'C': {'c': '1', 'd': '1'}, 'B': {'c': '1', 'b': '1', 'e': '1'}, 'E': {'a': '1', 'd': '1'}, 'D': {'c': '1', 'b': '1', 'd': '1'}}---2.構(gòu)造的共現(xiàn)矩陣--- ('N:', {'a': 2, 'c': 3, 'b': 3, 'e': 1, 'd': 4}) ('C', {'a': {'b': 1, 'd': 2}, 'c': {'b': 2, 'e': 1, 'd': 2}, 'b': {'a': 1, 'c': 2, 'e': 1, 'd': 2}, 'e': {'c': 1, 'b': 1}, 'd': {'a': 2, 'c': 2, 'b': 2}})---3.構(gòu)造的相似矩陣--- {'a': {'b': 0.4082482904638631, 'd': 0.7071067811865475}, 'c': {'b': 0.6666666666666666, 'e': 0.5773502691896258, 'd': 0.5773502691896258}, 'b': {'a': 0.4082482904638631, 'c': 0.6666666666666666, 'e': 0.5773502691896258, 'd': 0.5773502691896258}, 'e': {'c': 0.5773502691896258, 'b': 0.5773502691896258}, 'd': {'a': 0.7071067811865475, 'c': 0.5773502691896258, 'b': 0.5773502691896258}}---4.推薦---- [('c', 1.2440169358562925), ('e', 0.5773502691896258)]五、電影推薦應(yīng)用小案例
1.電影數(shù)據(jù)描述
電影數(shù)據(jù)很多,在此示范下基于物品的電影推薦,數(shù)據(jù)為基于用戶--->電影的倒排,數(shù)據(jù)如下 data = {'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}}2.推薦
推薦過程和上面的代碼過程一樣,僅不用構(gòu)造倒排數(shù)據(jù)
推薦結(jié)果:
---1.構(gòu)造數(shù)據(jù)------2.構(gòu)造的共現(xiàn)矩陣--- ('N:', {'Lady in the Water': 5, 'Snakes on a Plane': 7, 'Just My Luck': 4, 'Superman Returns': 7, 'The Night Listener': 6, 'You, Me and Dupree': 6}) ('C', {'Lady in the Water': {'Snakes on a Plane': 5, 'Just My Luck': 3, 'The Night Listener': 5, 'Superman Returns': 5, 'You, Me and Dupree': 4}, 'Snakes on a Plane': {'Lady in the Water': 5, 'Just My Luck': 4, 'The Night Listener': 6, 'Superman Returns': 7, 'You, Me and Dupree': 6}, 'Just My Luck': {'Lady in the Water': 3, 'Snakes on a Plane': 4, 'The Night Listener': 4, 'Superman Returns': 4, 'You, Me and Dupree': 4}, 'Superman Returns': {'Lady in the Water': 5, 'Snakes on a Plane': 7, 'The Night Listener': 6, 'Just My Luck': 4, 'You, Me and Dupree': 6}, 'The Night Listener': {'Lady in the Water': 5, 'Snakes on a Plane': 6, 'Just My Luck': 4, 'Superman Returns': 6, 'You, Me and Dupree': 5}, 'You, Me and Dupree': {'Lady in the Water': 4, 'Snakes on a Plane': 6, 'Just My Luck': 4, 'Superman Returns': 6, 'The Night Listener': 5}})---3.計算的相似矩陣--- {'Lady in the Water': {'Snakes on a Plane': 0.8451542547285166, 'Just My Luck': 0.6708203932499369, 'You, Me and Dupree': 0.7302967433402214, 'Superman Returns': 0.8451542547285166, 'The Night Listener': 0.9128709291752769}, 'Snakes on a Plane': {'Lady in the Water': 0.8451542547285166, 'Just My Luck': 0.7559289460184544, 'You, Me and Dupree': 0.9258200997725514, 'Superman Returns': 1.0, 'The Night Listener': 0.9258200997725514}, 'Just My Luck': {'Lady in the Water': 0.6708203932499369, 'Snakes on a Plane': 0.7559289460184544, 'You, Me and Dupree': 0.8164965809277261, 'Superman Returns': 0.7559289460184544, 'The Night Listener': 0.8164965809277261}, 'Superman Returns': {'Lady in the Water': 0.8451542547285166, 'Snakes on a Plane': 1.0, 'Just My Luck': 0.7559289460184544, 'You, Me and Dupree': 0.9258200997725514, 'The Night Listener': 0.9258200997725514}, 'You, Me and Dupree': {'Lady in the Water': 0.7302967433402214, 'Snakes on a Plane': 0.9258200997725514, 'Just My Luck': 0.8164965809277261, 'Superman Returns': 0.9258200997725514, 'The Night Listener': 0.8333333333333334}, 'The Night Listener': {'Lady in the Water': 0.9128709291752769, 'Snakes on a Plane': 0.9258200997725514, 'Just My Luck': 0.8164965809277261, 'Superman Returns': 0.9258200997725514, 'You, Me and Dupree': 0.8333333333333334}}---4.推薦---- [('The Night Listener', 8.70280418140002), ('Lady in the Water', 7.914107908532612), ('Just My Luck', 7.241892622084588)]總結(jié)
以上是生活随笔為你收集整理的(三)协同过滤算法之基于物品的推荐算法python实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (二)k-means算法原理以及pyth
- 下一篇: (四)协同过滤算法之基于用户的推荐算法p