roc_auc_score#
- sklearn.metrics.roc_auc_score(y_true, y_score, *, average='macro', sample_weight=None, max_fpr=None, multi_class='raise', labels=None)[源代码]#
计算受试者工作特征曲线下面积(ROC AUC) 来自预测分数。
注意:此实现可用于二进制,多类和多标签分类,但有一些限制(请参阅参数)。
阅读更多的 User Guide .
- 参数:
- y_true形状类似阵列(n_samples,)或(n_samples,n_classes)
真标签或二进制标签指示符。二进制和多类情况下期望具有形状(n_samples,)的标签,而多标签情况下期望具有形状(n_samples,n_classes)的二进制标签指示符。
- y_score形状类似阵列(n_samples,)或(n_samples,n_classes)
目标分数。
在二进制情况下,它对应于形状数组
(n_samples,)
.可以提供概率估计和非阈值决策值。概率估计对应于 probability of the class with the greater label ,即estimator.classes_[1]
并且因此estimator.predict_proba(X, y)[:, 1]
.决策值对应于estimator.decision_function(X, y)
.请参阅更多信息 User guide ;在多类的情况下,它对应于一个形状数组
(n_samples, n_classes)
提供的概率估计predict_proba
法述概率估计 must 在可能的类中总和为1。此外,班级成绩的顺序必须与labels
,如果提供的话,或者按照中标签的数字或词典顺序排列y_true
.请参阅更多信息 User guide ;在多标签情况下,它对应于形状数组
(n_samples, n_classes)
.概率估计由predict_proba
方法和非阈值决策值decision_function
法概率估计对应于 probability of the class with the greater label for each output 分类器的。请参阅更多信息 User guide .
- average“微观”、“宏观”、“样本”、“加权”}或无, 默认='宏'
如果
None
,返回每个班级的成绩。否则,这将确定对数据执行的平均类型。注意:多类ROC AUC目前仅处理“宏观”和“加权”平均值。对于多类目标,average=None
仅适用于multi_class='ovr'
和average='micro'
仅适用于multi_class='ovr'
.'micro'
:通过将标签指标矩阵的每个元素视为一个标签来全局计算指标。
'macro'
:计算每个标签的指标,并找到其未加权平均值。 这没有考虑标签不平衡。
'weighted'
:计算每个标签的指标,并找到其平均值,并通过支持度(每个标签的真实实例数量)进行加权。
'samples'
:计算每个实例的指标,并找到其平均值。
将被忽视时
y_true
是二进制的。- sample_weight形状类似数组(n_samples,),默认=无
样本重量。
- max_fprfloat > 0且<= 1,默认=无
如果不是
None
,标准化部分AUR [2] 范围内 [0, max_fpr] 已经回来了对于多类情况,max_fpr
,应该等于None
或1.0
因为目前多类不支持AUCROC部分计算。- multi_class请输入'raise','ovr','ovo'},default='raise'
仅用于多类目标。确定要使用的配置类型。默认值会引发错误,因此
'ovr'
或'ovo'
必须明确通过。- labels形状类似数组(n_classes,),默认=无
仅用于多类目标。对中的类进行索引的标签列表
y_score
.如果None
,中标签的数字或词典顺序y_true
采用了
- 返回:
- auc浮子
曲线下面积评分。
参见
average_precision_score
准确率-召回曲线下面积。
roc_curve
计算接收器工作特性(ROC)曲线。
RocCurveDisplay.from_estimator
绘制给定估计量和一些数据的受试者工作特征(ROC)曲线。
RocCurveDisplay.from_predictions
在给定真实值和预测值的情况下绘制接收器工作特征(ROC)曲线。
注意到
基尼系数是二进制分类器排名能力的总结衡量标准。使用中华民国下的面积表示如下:
G = 2 * UC- 1
其中G是基尼系数,AUC是ROC-AUC评分。这种标准化将确保随机猜测的期望分数为0,并且上限为1。
引用
[1]Wikipedia entry for the Receiver operating characteristic <https://en.wikipedia.org/wiki/Receiver_operating_characteristic>
_[2]Analyzing a portion of the ROC curve. McClish, 1989 <https://www.ncbi.nlm.nih.gov/pubmed/2668680>
_[3]教务长,F.,Domingos,P.(2000年)。训练有素的PET:改进概率估计树(第6.2节),CeDER工作文件#IS-00-04,纽约大学斯特恩商学院。
[4]Fawcett, T. (2006). An introduction to ROC analysis. Pattern Recognition Letters, 27(8), 861-874. <https://www.sciencedirect.com/science/article/pii/S016786550500303X>
_[5]Hand, D.J., Till, R.J. (2001). A Simple Generalisation of the Area Under the ROC Curve for Multiple Class Classification Problems. Machine Learning, 45(2), 171-186. <http://link.springer.com/article/10.1023/A:1010920819831>
_[6]Wikipedia entry for the Gini coefficient <https://en.wikipedia.org/wiki/Gini_coefficient>
_示例
二元案例:
>>> from sklearn.datasets import load_breast_cancer >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.metrics import roc_auc_score >>> X, y = load_breast_cancer(return_X_y=True) >>> clf = LogisticRegression(solver="liblinear", random_state=0).fit(X, y) >>> roc_auc_score(y, clf.predict_proba(X)[:, 1]) np.float64(0.99...) >>> roc_auc_score(y, clf.decision_function(X)) np.float64(0.99...)
多类案例:
>>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> clf = LogisticRegression(solver="liblinear").fit(X, y) >>> roc_auc_score(y, clf.predict_proba(X), multi_class='ovr') np.float64(0.99...)
多标签病例:
>>> import numpy as np >>> from sklearn.datasets import make_multilabel_classification >>> from sklearn.multioutput import MultiOutputClassifier >>> X, y = make_multilabel_classification(random_state=0) >>> clf = MultiOutputClassifier(clf).fit(X, y) >>> # get a list of n_output containing probability arrays of shape >>> # (n_samples, n_classes) >>> y_pred = clf.predict_proba(X) >>> # extract the positive columns for each output >>> y_pred = np.transpose([pred[:, 1] for pred in y_pred]) >>> roc_auc_score(y, y_pred, average=None) array([0.82..., 0.86..., 0.94..., 0.85... , 0.94...]) >>> from sklearn.linear_model import RidgeClassifierCV >>> clf = RidgeClassifierCV().fit(X, y) >>> roc_auc_score(y, clf.decision_function(X), average=None) array([0.81..., 0.84... , 0.93..., 0.87..., 0.94...])