Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. # person以外の全ユーザの評点の重み付き平均を使い、personへの推薦を算出する
  2. def getRecommendations(prefs,person,similarity=sim_pearson):
  3. totals={}
  4. simSums={}
  5. for other in prefs:
  6. # 自分自身とは比較しない
  7. if other==person: continue
  8. sim=similarity(prefs,person,other)
  9.  
  10. # 0以下のスコアは無視する
  11. if sim<=0: continue
  12.  
  13. for item in prefs[other]:
  14. # まだ見ていない映画の得点のみを算出
  15. if item not in prefs[person] or prefs[person][item]==0:
  16. # 類似性 * スコア
  17. totals.setdefault(item,0)
  18. totals[item]+=prefs[other][item]*sim
  19. # 類似度を合計
  20. simSums.setdefault(item,0)
  21. simSums[item]+=sim
  22. # 正規化したリストを作る
  23. rankings=[(total/simSums[item],item)for item, total in totals.items()]
  24.  
  25. # ソート済みのリストを返す
  26. rankings.sort()
  27. rankings.reverse()
  28. return rankings
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement