一、准确率(Precision)和召回率(Recall)
(令R(u)是根据用户在训练集上的行为给用户作出的推荐列表,而T(u)是用户在测试集上的行为列表。)
对用户u推荐N个物品(记为R(u)),令用户u在测试集上喜欢的物品集合为T(u),然后可以通过准确率/召回率评测推荐算法的精度:
准确率描述最终的推荐列表中有多少比例是发生过的用户—物品评分记录;
召回率描述有多少比例的用户—物品评分记录包含在最终的推荐列表中。
准确率和召回率计算方法的Python代码如下:
defRecall(train,test,N):
hit=0
all=0for user intrain.keys():
Tu=test[user]
rank=GetRecommendation(user,N)for item,pui inrank:if item inTu:
hit+=1all+=len(Tu)return hit/(all*1.0)defPrecision(train,test,N):
hit=0
all=0for user intrain.keys():
Tu=test[user]
rank=GetRecommendation(user,N)for item,pui inrank:if item inTu:
hit+=1all+=Nreturn hit/(all*1.0)
View Code
下面的Python代码同时计算出了一个推荐算法的准确率和召回率:
defPrecisionRecall(test, N):
hit=0
n_recall=0
n_precision=0for user, items intest.items():
rank=Recommend(user, N)
hit+= len(rank &items)
n_recall+=len(items)
n_precision+=Nreturn [hit / (1.0 * n_recall), hit / (1.0 * n_precision)]
View Code
有的时候,为了全面评测TopN推荐的准确率和召回率,一般会选取不同的推荐列表长度N,计算出一组准确率/召回率,然后画出准确率/召回率曲线(precision/recall curve)。
二、Mean average precision(MAP)&#x