roc_curve#
- sklearn.metrics.roc_curve(y_true, y_score, *, pos_label=None, sample_weight=None, drop_intermediate=True)[source]#
计算接收者操作特征 (ROC)。
注意:通过一对多或一对一方式,不支持超出二元分类任务的扩展。
在用户指南中阅读更多内容。
- 参数:
- y_true形状为 (n_samples,) 的 array-like
真实的二元标签。如果标签不是 {-1, 1} 或 {0, 1},则应明确给出 pos_label。
- y_score形状为 (n_samples,) 的类数组
目标得分,可以是正类的概率估计、置信度值,或非阈值化的决策度量(如某些分类器返回的“decision_function”)。对于decision_function得分,大于或等于零的值应指示正类。
- pos_labelint, float, bool or str, default=None
正类的标签。当
pos_label=None时,如果y_true在{-1, 1}或{0, 1}中,则pos_label设置为1,否则将引发错误。- sample_weightshape 为 (n_samples,) 的 array-like, default=None
样本权重。
- drop_intermediatebool, default=True
是否删除在ROC空间中与邻居共线的阈值点。这不影响ROC AUC或曲线的视觉形状,但会减少绘制点的数量。
版本 0.17 中新增: 参数 drop_intermediate。
- 返回:
- fprndarray of shape (>2,)
递增的假正率,其中元素
i是得分 >=thresholds[i]的预测的假正率。- tprndarray of shape (>2,)
递增的真正率,其中元素
i是得分 >=thresholds[i]的预测的真正率。- thresholds形状为 (n_thresholds,) 的 ndarray
用于计算fpr和tpr的决策函数上的递减阈值。第一个阈值设置为
np.inf。版本 1.3 中更改: 添加了一个任意的无穷大阈值(存储在
thresholds[0]中),以表示一个总是预测负类的分类器,即fpr=0且tpr=0。
另请参阅
RocCurveDisplay.from_estimator给定一个估计器和一些数据,绘制接收者操作特征 (ROC) 曲线。
RocCurveDisplay.from_predictions给定真实值和预测值,绘制接收者操作特征 (ROC) 曲线。
RocCurveDisplay.from_cv_results根据交叉验证结果绘制多折ROC曲线。
det_curve计算不同概率阈值的错误率。
roc_auc_score计算 ROC 曲线下的面积。
confusion_matrix_at_thresholds对于二分类,计算每个阈值的真阴性、假阳性、假阴性和真阳性计数。
注意事项
由于阈值是从低到高排序的,在返回时它们被反转以确保它们对应于
fpr和tpr,后者在计算过程中按相反顺序排序。References
[1][2]Fawcett T. An introduction to ROC analysis[J]. Pattern Recognition Letters, 2006, 27(8):861-874.
示例
>>> import numpy as np >>> from sklearn import metrics >>> y = np.array([1, 1, 2, 2]) >>> scores = np.array([0.1, 0.4, 0.35, 0.8]) >>> fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2) >>> fpr array([0. , 0. , 0.5, 0.5, 1. ]) >>> tpr array([0. , 0.5, 0.5, 1. , 1. ]) >>> thresholds array([ inf, 0.8 , 0.4 , 0.35, 0.1 ])