def Coverage(train, test, W, K, r, N): recommend_items = set() all_items = set() for user in train: for item in train[user]: all_items.add(item) rank = GetRecommendation(user, train, W, K, r) rec_item = sorted(rank.items(), key=lambda x: x[1], reverse=True)[0:N] # 推荐感兴趣程度前N的item for item, pui in rec_item: recommend_items.add(item) return len(recommend_items) / (len(all_items) * 1.0)
def Precision(train, test, W, K, r, N): hit = 0 n_precision = 0 for user in train: tu = test[user] rank = GetRecommendation(user, train, W, K, r) rec_item = sorted(rank.items(), key=lambda x: x[1], reverse=True)[0:N] # 推荐感兴趣程度前N的item for item, pui in rec_item: if item in tu: hit += 1 n_precision += N return hit / (n_precision * 1.0)
def Recall(train, test, W, K, r, N): hit = 0 n_recall = 0 print("召回率计算开始") for user in train: tu = test[user] rank = GetRecommendation(user, train, W, K, r) rec_item = sorted(rank.items(), key=lambda x: x[1], reverse=True)[0:N] # 推荐感兴趣程度前N的item for item, pui in rec_item: if item in tu: hit += 1 n_recall += len(tu) print("召回率计算完成") return hit / (n_recall * 1.0)
def Popularity(train, test, W, K, r, N): item_popularity = dict() # item的流行度 for user in train: for item in train[user]: if item not in item_popularity: item_popularity[item] = 1 else: item_popularity[item] += 1 ret = 0 n = 0 for user in train: rank = GetRecommendation(user, train, W, K, r) rec_item = sorted(rank.items(), key=lambda x: x[1], reverse=True)[0:N] # 推荐感兴趣程度前N的item for item, pui in rec_item: ret += math.log(1 + item_popularity[item]) # 物品流行度分布满足长尾分布,取对数后均值更加平稳 n += 1 ret /= (n * 1.0) return ret
print("交叉验证集:", len(cv)) #print("测试集:",test["abin520918"]) print("相似性计算开始") # UserCF,用户相似度选择 W = InvertedIndex(train) # 余弦相似度,倒排表实现 # W = ImprovedSimilarity(train) # 改进的余弦相似度 #W = Dist_Similarity(train, r) # 基于距离的相似度 # W = FusionSimilarity(w, dist) # 基于距离和余弦相似度的加权相似度 # W = FusionSimilarity(w, dist) # 基于距离和改进余弦相似度的加权相似度 print("相似性计算完成") N = 10 K = 40 for user in train: user_recommend = [] recommend_items = set() rank = GetRecommendation(user, train, W, K, r) rec_item = sorted(rank.items(), key=lambda x: x[1], reverse=True)[0:N] for item, pui in rec_item: recommend_items.add(item) #print("recommend_items",recommend_items) user_recommend.append(user) user_recommend.extend(recommend_items) df2 = pd.DataFrame(user_recommend) df2 = df2.T #print(df2) df2.to_csv('result.csv', mode='a', header=False, index=False) #print("user_recommend",user_recommend) #print("shape:",np.array(user_recommend).shape) """ listN = [20, 40, 60] # 给用户u推荐的item个数