python - 如何在python中计算2个列表的精度和召回率
问题描述
我写了一个电影推荐系统。我有我推荐给用户的 20 部电影的列表和用户最后真正看过的 150 部电影的列表。如何在 python 中使用 sklearn 计算这两个列表中的精度和召回率?
比如我有10部电影推荐给用户,用户真正看过,recall的计算是:10/150,precision的计算是:10/20
解决方案
根据我的阅读,最简单的方法是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
推荐阅读
- javascript - 如何在 ReactJS 中将数据 yyyy-mm-dd 转换为 dd/mm/yyyy?
- swift - Swift 错误代码:实例成员 'getStory' 不能用于类型 'StoryBrain';你的意思是使用这种类型的值吗?
- excel - 如何在excel中提取满足多行多列条件的值?
- python - 展开数组并追加行名
- python - 将字典的字典与另一个字典的字典添加顺序不正确
- rubygems - Jekyll + Github Actions + JavaScript 运行时问题
- javascript - 通过在页面加载后切换状态来更改样式化组件样式
- node.js - 从 Nodejs 中的 Express 中提取 POST 请求数据
- django - Django:从不同的表中获取值并计算字段
- r - 使用 dplyr 对每行(按行)中的特定字符串(字符串)进行计数或求和