如何在Python中计算2个列表的精度和召回率

eli*_*oen 1 python scikit-learn precision-recall

我写了一个电影推荐系统。我有向用户推荐的 20 部电影列表和用户最后真正看过的 150 部电影列表。如何使用 sklearn 在 python 中计算这两个列表中的精度和召回率?

例如我向用户推荐了10部用户真正看过的电影,Recall的计算是:10/150,Precision的计算是:10/20

Plo*_*opp 7

intersection根据我的阅读,最简单的方法是在两组之间使用。

我想您对电影使用某种标识符,因此您的列表不能有重复项(例如,您可能不会推荐同一部电影两次),这意味着您可以使用集合及其内置的intersection.

recommendations={"movie1", "movie2", "movie3"}
saw={"movie1", "movie2", "movie4", "movie5", "movie6"}

"recommended movies saw by the user"
recommendations.intersection(saw)
>>> {"movie1", "movie2"}

# To get the "number of recommended movie that the user saw":
movie_intersect = len(recommendations.intersection(saw))
movie_intersect
>>> 2

# Precision is just:
movie_intersect/len(recommendations)
>>> 0.666666666666666667

# Recall:
movie_intersect/len(saw)
>>> 0.4
Run Code Online (Sandbox Code Playgroud)