BLOGLLM Logo
Go Pro
Guest
Not signed in
所有博客课程练习面试准备系列标签书签价格
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboard游戏
Articles
练习 / Evaluation metrics9 of 33 in topic

160. 通过统计排序对来计算 ROC AUC

MediumEvaluation metrics
提问于ByteDanceGoogleMicrosoft

ROC AUC 有一个直观的含义:随机选择一个正样本和一个负样本;AUC 即正样本获得更高评分的概率。平局计为 0.5。

为二分类标签编写 roc_auc(y_true, scores) 函数。以 float 类型返回 AUC。如果没有正样本或没有负样本,返回 0.5。

函数def roc_auc(y_true, scores)
Play with it

ROC curve and AUC

Each threshold gives one point (false positive rate, true positive rate). Better separated classes push the curve to the top left.

0102000.20.40.60.81scorecount00.250.50.75100.20.40.60.81false positive ratetrue positive rate
predicted 1predicted 0
actual 1TP 114FN 36
actual 0FP 33TN 117
precision
0.776
recall
0.76
F1
0.768
MCC
0.54
ROC AUC
0.849
class 1class 0

Synthetic scores made in your browser: 150 per class, sigmoid of a normal value centred at ±separation / 2. AUC counts every positive and negative pair.

示例 1

输入: y_true = [0, 0, 1, 1], scores = [0.1, 0.2, 0.8, 0.9]

输出: 1.0

示例 2

输入: y_true = [0, 1, 0, 1], scores = [0.5, 0.5, 0.2, 0.9]

输出: 0.875

示例 3

输入: y_true = [1, 0, 1, 0, 1, 0], scores = [0.3, 0.6, 0.7, 0.2, 0.4, 0.4]

输出: 0.611111

约束条件

  • Python 3 standard library. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
提示 1

遍历每一个 (positive, negative) 对:如果正样本的 score 更高则加 1,如果相等则加 0.5。

提示 2

除以 n_pos * n_neg。

提示 3

该方法适用于小规模输入。

常见错误
  • 忽略平局(错误的实现方式)会低估那些输出粗粒度 score 的模型的 AUC。
理解思路

ROC AUC 有一个简单的含义:随机选择一个正样本和一个负样本;AUC 是正样本获得更高评分的概率。因此,你可以通过检查每一对正负样本来计算它。平局就像抛硬币一样,计为 0.5。就像一场锦标赛,每个正样本都与每个负样本对阵,然后统计胜场。

Worked example

labels [1, 0, 1, 0], scores [0.8, 0.4, 0.4, 0.2]。正样本的 scores 为 0.8 和 0.4;负样本的 scores 为 0.4 和 0.2。样本对:0.8 vs 0.4 胜,0.8 vs 0.2 胜,0.4 vs 0.4 平局 (0.5),0.4 vs 0.2 胜。总计 3.5 / 4 对,所以 AUC = 0.875。AUC 为 0.5 代表随机猜测,1.0 代表完美排序。如果没有正样本或没有负样本,返回 0.5。

AUC = (wins + 0.5 x ties) / (n_pos x n_neg)。它是欺诈检测或点击率模型等排序类分类器的常用指标。
主题
Evaluation metrics
公司3
  • ByteDance the topic appears in interview reports ByteDance Machine Learning Engineer Interview Process
  • Google the topic appears in interview reports Google Machine Learning Engineer Interview Guide (Interview Query)
  • Microsoft a candidate reported this question Microsoft Machine Learning Engineer Interview Questions | Screna AI

来自公开面试报告和指南。与这些公司无隶属关系或背书关系。

相似题目
  • 使用 NDCG@k 对搜索排名进行评分Medium
  • 计算多分类分类器的 macro 和 micro F1Medium
  • 使用 Cohen's kappa 衡量评分者一致性Medium
  • 无偏估计 pass@kMedium
所有 Evaluation metrics 道题目
反馈
160 / 903
Python 3solution.pyPython 空闲
1
2
3
4
def roc_auc(y_true, scores):
# your code here
pass
​

Run 会在浏览器中检查这些用例,无需登录。 Submit 会检查所有 4 个测试。

Ctrl/按 + 回车 运行
© BlogLLM, Bengaluru, India关于条款隐私退款DMCA联系我们RSS