sklearn 分类指标 auc return ValueError

sklearn classification metric auc return ValueError

我正在使用 KNN

构建两个 class class化模型

我试着用

计算auc_score
from sklearn.metrics import auc

auc(y_test, y_pred)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-183-980dc3c4e3d7> in <module>
----> 1 auc(y_test, y_pred)

~/.local/lib/python3.6/site-packages/sklearn/metrics/ranking.py in auc(x, y, reorder)
    117             else:
    118                 raise ValueError("x is neither increasing nor decreasing "
--> 119                                  ": {}.".format(x))
    120 
    121     area = direction * np.trapz(y, x)

ValueError: x is neither increasing nor decreasing : [1 1 1 ... 1 1 1].

然后我用了roc_auc_score

from sklearn.metrics import roc_auc_score
roc_auc_score(y_test, y_pred)
0.5118361429056588

为什么 aucroc_auc_score 正常工作的地方不起作用。我以为他们都是一样的?我在这里错过了什么?

这里y_test是实际目标值,y_pred是我的预测值。

它们在实现和含义上是不同的:

auc:

Compute Area Under the Curve (AUC) using the trapezoidal rule. This is a general function, given points on a curve.

roc_auc_score:

Compute Area Under the Receiver Operating Characteristic Curve (ROC AUC) from prediction scores.

意味着aucroc_auc_score更通用,虽然你可以从auc得到相同的roc_auc_curve值。因此,auc的输入参数是指定曲线的xy坐标,你的错误来自于必要输入类型的不同!此外,xy 必须按升序或降序排列。

AUC is used most of the time to mean AUROC, which is a bad practice since as Marc Claesen pointed out AUC is ambiguous (could be any curve) while AUROC is not.

  • 对于二进制分类,您需要使用指标 ROC AUC 而不是面积 在曲线下。

至于为什么AUC出现数值错误是因为下面的错误

x is neither increasing nor decreasing : [1 1 1 ... 1 1 1]

auc metric uses trapezoid rule 近似曲线下面积和梯形规则需要规则间隔采样函数,即它需要输入如下函数 y = exp(x^2)

X : 0.0, 0.1, 0.2, 0.3, 0.4

Y : 1.00000 1.01005 1.04081 1.09417 1.17351

因此 X 应该是单调递增或单调递减,而 Y 只是函数在该点的输出。