轉利用python實現電影推薦


“協同過濾”是推薦系統中的常用技術,按照分析維度的不同可實現“基於用戶”和“基於產品”的推薦。

以下是利用python實現電影推薦的具體方法,其中數據集源於《集體編程智慧》一書,后續的編程實現則完全是自己實現的(原書中的實現比較支離、難懂)。

這里我采用的是“基於產品”的推薦方法,因為一般情況下,產品的種類往往較少,而用戶的數量往往非常多,“基於產品”的推薦程序可以很好的減小計算量。

 

其實基本的思想很簡單:

首先讀入數據,形成用戶-電影矩陣,如圖所示:矩陣中的數據為用戶(橫坐標)對特定電影(縱坐標)的評分。

其次根據用戶-電影矩陣計算不同電影之間的相關系數(一般用person相關系數),形成電影-電影相關度矩陣。

其次根據電影-電影相關度矩陣,以及用戶已有的評分,通過加權平均計算用戶未評分電影的預估評分。例如用戶對A電影評3分、B電影評4分、C電影未評分,而C電影與A電影、B電影的相關度分別為0.3和0.8,則C電影的預估評分為(0.3*3+0.8*4)/(0.3+0.8)。

最后對於每一位用戶,提取其未評分的電影並按預估評分值倒序排列,提取前n位的電影作為推薦電影。

 

以下為程序源代碼,大塊的注釋還是比較詳細的,便於理解各個模塊的作用。此外,程序用到了pandas和numpy庫,實現起來會比較簡潔,因為許多功能如計算相關系數、排序等功能在這些庫中已有實現,直接拿來用即可。

 

[python]  view plain  copy
 
  1. import pandas as pd  
  2. import numpy as np  
  3.   
  4. #read the data  
  5. data={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,  
  6.  'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5},  
  7. 'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5,   
  8.  'Just My Luck': 1.5, 'The Night Listener': 3.0},   
  9. 'Michael Phillips': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,  
  10.  'Superman Returns': 3.5, 'The Night Listener': 4.0},  
  11. 'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,  
  12.  'The Night Listener': 4.5, 'You, Me and Dupree': 2.5},  
  13. 'Mick LaSalle': {'Just My Luck': 2.0, 'Lady in the Water': 3.0,'Superman Returns': 3.0, 'The Night Listener': 3.0, 'You, Me and Dupree': 2.0},   
  14. 'Jack Matthews': {'Snakes on a Plane': 4.0, 'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},  
  15. 'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}}  
  16.   
  17. #clean&transform the data  
  18. data = pd.DataFrame(data)  
  19. #0 represents not been rated  
  20. data = data.fillna(0)  
  21. #each column represents a movie  
  22. mdata = data.T  
  23.   
  24. #calculate the simularity of different movies, normalize the data into [0,1]  
  25. np.set_printoptions(3)  
  26. mcors = np.corrcoef(mdata, rowvar=0)  
  27. mcors = 0.5+mcors*0.5  
  28. mcors = pd.DataFrame(mcors, columns=mdata.columns, index=mdata.columns)  
  29.   
  30. #calculate the score of every item of every user  
  31. #matrix:the user-movie matrix  
  32. #mcors:the movie-movie correlation matrix  
  33. #item:the movie id  
  34. #user:the user id  
  35. #score:score of movie for the specific user   
  36. def cal_score(matrix,mcors,item,user):  
  37.     totscore = 0  
  38.     totsims = 0  
  39.     score = 0  
  40.     if pd.isnull(matrix[item][user]) or matrix[item][user]==0:  
  41.         for mitem in matrix.columns:  
  42.             if matrix[mitem][user]==0:  
  43.                 continue  
  44.             else:  
  45.                 totscore += matrix[mitem][user]*mcors[item][mitem]  
  46.                 totsims += mcors[item][mitem]  
  47.         score = totscore/totsims  
  48.     else:  
  49.         score = matrix[item][user]  
  50.     return score  
  51.   
  52. #calculate the socre matrix  
  53. #matrix:the user-movie matrix  
  54. #mcors:the movie-movie correlation matrix  
  55. #score_matrix:score matrix of movie for different users   
  56. def cal_matscore(matrix,mcors):  
  57.     score_matrix = np.zeros(matrix.shape)  
  58.     score_matrix = pd.DataFrame(score_matrix, columns=matrix.columns, index=matrix.index)  
  59.     for mitem in score_matrix.columns:  
  60.         for muser in score_matrix.index:  
  61.             score_matrix[mitem][muser]  = cal_score(matrix,mcors,mitem,muser)  
  62.     return score_matrix  
  63.   
  64. #give recommendations: depending on the score matrix  
  65. #matrix:the user-movie matrix  
  66. #score_matrix:score matrix of movie for different users   
  67. #user:the user id  
  68. #n:the number of recommendations  
  69. def recommend(matrix,score_matrix,user,n):  
  70.     user_ratings = matrix.ix[user]  
  71.     not_rated_item = user_ratings[user_ratings==0]  
  72.     recom_items = {}  
  73.     #recom_items={'a':1,'b':7,'c':3}  
  74.     for item in not_rated_item.index:  
  75.         recom_items[item] = score_matrix[item][user]  
  76.     recom_items = pd.Series(recom_items)  
  77.     recom_items = recom_items.sort_values(ascending=False)  
  78.     return recom_items[:n]    
  79.   
  80.   
  81. #main  
  82. score_matrix = cal_matscore(mdata,mcors)  
  83. for i in range(10):  
  84.     user = input(str(i)+' please input the name of user:')  
  85.     print recommend(mdata,score_matrix,user,2) 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM