AUC ROC

本文深入探讨了机器学习中ROC曲线的绘制方法及AUC评分的计算过程,通过具体实例展示了不同阈值下真阳性率与假阳性率的变化,帮助读者理解模型性能评估的关键指标。

在这里插入图片描述
在这里插入图片描述

In [1]: import numpy as np
In [2]: from sklearn.metrics import roc_auc_score
In [3]: y_true = np.array([0, 0, 1, 1])
In [4]: y_scores = np.array([0.1, 0.4, 0.35, 0.8])
In [5]: roc_auc_score(y_true, y_scores)
Out[5]: 0.75

阈值为:0.1时,点(1,1) (fpr,tpr)
阈值为:0.35时,点(0.5,1)
阈值为:0.4时,点(0.5,0.5)
阈值为:0.8时,点(0,0.5)

In [6]: import numpy as np
In [7]: from sklearn import metrics
In [8]: y = np.array([1,1,2,2]) ## 没有"0"
In [9]: scores = np.array([0.1,0.4,0.35,0.8])
In [10]: fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2)
In [11]: fpr
Out[11]: array([0. , 0. , 0.5, 0.5, 1. ])
In [12]: tpr
Out[12]: array([0. , 0.5, 0.5, 1. , 1. ]
In [13]: thresholds
Out[13]: array([1.8 , 0.8 , 0.4 , 0.35, 0.1 ])

在这里插入图片描述

In [1]: y = [2,2,1,2,2,2,1,1,2,1,2,1,2,1,1,1,2,1,2,1]

In [2]: len(y)
Out[2]: 20

In [3]: x = [0.9,.8,.7,.6,.55,.54,.53,.52,.51,.505,.4,.39,.38,.37,.36,.35,.34,.33,.3,.1]

In [4]: len(x)
Out[4]: 20

In [5]: import numpy as np

In [6]: y_label = np.array(y)

In [7]: x_scores = np.array(x)

In [8]: from sklearn import metrics

In [9]: fpr, tpr, thresholds = metrics.roc_curve(y_label, x_scores,pos_label=2)

In [10]: fpr
Out[10]:
array([0. , 0. , 0. , 0.1, 0.1, 0.3, 0.3, 0.4, 0.4, 0.5, 0.5, 0.8, 0.8,
       0.9, 0.9, 1. ])

In [11]: tpr
Out[11]:
array([0. , 0.1, 0.2, 0.2, 0.5, 0.5, 0.6, 0.6, 0.7, 0.7, 0.8, 0.8, 0.9,
       0.9, 1. , 1. ])

In [12]: thresholds
Out[12]:
array([1.9  , 0.9  , 0.8  , 0.7  , 0.54 , 0.52 , 0.51 , 0.505, 0.4  ,
       0.39 , 0.38 , 0.35 , 0.34 , 0.33 , 0.3  , 0.1  ])

In [13]: import matplotlib.pyplot as plt

In [14]: plt.plot(fpr, tpr)
Out[14]: [<matplotlib.lines.Line2D at 0x2b1d1f2f0b8>]

In [15]: plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值