Search code examples
pythonpython-3.xmachine-learningscikit-learnauc

sklearn classification metric auc return ValueError


I'm building a two class classification model using KNN

I tried to calculate auc_score with

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].

Then I used roc_auc_score

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

Why is it auc is not working where as roc_auc_score is working. I though they both were same? What am I missing here?

Here y_test is actual target values and y_pred is my predicted values.


Solution

  • They are different in implementation and meaning:

    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.

    It means auc is more general than roc_auc_score, although you can get the same value of roc_auc_curve from auc. Hence, the input parameter of the auc is the x and y coordinates of the specified curve, and your error comes from the difference in types of necessary input! Also, the x and y must be in an increasing or decreasing order.