在開源框架taste中有SlopOne的Java實現,效果不錯。使用movielens的數據,代碼例如如下python
代碼框架
#coding:utf-8 import re import math #讀取數據,並生成矩陣 def getMatrix(): mat = {} f = open("u.data", "r") for line in f: list = map(int, line.strip("\n").split("\t")) if list[0] in mat: mat[ list[0] ][ list[1] ] = list[2] else: mat[ list[0] ] = { list[1] : list[2] } f.close() return mat #計算某個用戶的平均分 def getAvg(usr): res = 0.0 for i in usr: res += usr[i] return res / len(usr) #預測分數, 返回矩陣mat中用戶usr對item的評分 def getSlopOne(mat, user, item): #用戶user的所有item的列表 list = mat[user] #分子 mole = 0.0 #分母 demo = 0.0 #對於每一個物品。都計算它和物品item的差值,終於算出相對它item的score for it in list: diff_sum = 0.0 user_num = 0 for us in mat: us_it = mat[us] #假設該user同一時候評價過這兩個item,則採納他的評分 if item in us_it and it in us_it: diff_sum += us_it[item] - us_it[it] user_num += 1 #假設item被人評價過 if user_num: diff_avg = diff_sum / user_num mole += (list[it] + diff_avg) * user_num demo += user_num #假設沒有人評價過,則取這我的的平均分 if user_num == 0: return getAvg(list) #不然返回終於的score return mole / demo def main(): mat = getMatrix() rf = open("u.data", "r") wf = open("o.data", "w") for line in rf: list = map(int, line.strip("\n").split("\t")) score = getSlopOne(mat, list[0], list[1]) output = str(list[0]) + "\t" + str(list[1]) + "\t" + str(list[2]) + "\t" + str(score) + "\n" wf.write(output) rf.close() wf.close() if __name__ == "__main__": main()
版權聲明:本文博主原創文章,博客,未經贊成不得轉載。spa