推荐系统评测指标之RMSE、MSE

# -*- coding:utf-8 -*-
from loadMovieLens import *
import math
'''
评测指标
RMSE:均方根误差
MAE:平均绝对误差
'''
def getRMSE(prefer1,prefer2):
    rmse = 0
    for key1,value in prefer1.items():
        zonghe = 0
        #迭代测试集,并与结果集进行比较
        for key,value in prefer1[key1].items():
            temp = pow(value - prefer2[key1][key],2)#算两点的平方
            zonghe += temp
        rmse += math.sqrt(zonghe)/len(prefer1[key1])
    return rmse/len(prefer1)
def getMSE(prefer1,prefer2):
    mse = 0
    for key1,value in prefer1.items():
        zonghe = 0
        #迭代测试集,并与结果集进行比较
        for key,value in prefer1[key1].items():
            temp = abs(value - prefer2[key1][key])#计算两点之间的绝对值
            zonghe += temp
        mse += zonghe/float(len(prefer1[key1]))
    return mse/float(len(prefer1))
if __name__=='__main__':
    prefer1 = loadMovieLensTest()
    prefer2 = loadResult("D:/数据集/movieLens/result.txt")
    #print getRMSE(prefer1,prefer2)
    print len(prefer1)
    print getMSE(prefer1,prefer2)





你可能感兴趣的:(Python数据分析)