>符号不匹配问题 ,改了好多地方都没有用
def evaluate_recommendations(test_set, userma_to_num, data_handling, predict, k=10):
total_TP = 0
total_FP = 0
total_FN = 0
for user_id in test_set.index:
recommendations = recommend_movies(user_id, userma_to_num, data_handling, predict, k)
actual_liked_movies = test_set.loc[user_id][test_set.loc[user_id] > 0].index.tolist()
TP_user = 0
FP_user = 0
FN_user = 0
for movie_id in recommendations:
if movie_id in actual_liked_movies:
TP_user += 1
else:
FP_user += 1
for movie_id in actual_liked_movies:
if movie_id not in recommendations:
FN_user += 1
total_TP += TP_user
total_FP += FP_user
total_FN += FN_user
precision = total_TP / (total_TP + total_FP) if (total_TP + total_FP) != 0 else 0
recall = total_TP / (total_TP + total_FN) if (total_TP + total_FN) != 0 else 0
f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) != 0 else 0
return precision, recall, f1_score