Skip to content

Commit

Permalink
Merge 4c4ea88 into 6b130f1
Browse files Browse the repository at this point in the history
  • Loading branch information
qinhanmin2014 committed Oct 10, 2017
2 parents 6b130f1 + 4c4ea88 commit ade8eaf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
7 changes: 7 additions & 0 deletions doc/whats_new/v0.20.rst
Expand Up @@ -124,3 +124,10 @@ Linear, kernelized and related models
- Deprecate ``random_state`` parameter in :class:`svm.OneClassSVM` as the
underlying implementation is not random.
:issue:`9497` by :user:`Albert Thomas <albertcthomas>`.

Metrics

- Deprecate ``reorder`` parameter in :class:`metrics.auc` as the result
from auc will be significantly influenced if x is sorted unexpectedly
due to slight floating point error.
:issue:`9851` by :user:`Hanmin Qin <qinhanmin2014>`.
30 changes: 22 additions & 8 deletions sklearn/metrics/ranking.py
Expand Up @@ -36,7 +36,7 @@
from .base import _average_binary_score


def auc(x, y, reorder=False):
def auc(x, y, reorder='deprecated'):
"""Compute Area Under the Curve (AUC) using the trapezoidal rule
This is a general function, given points on a curve. For computing the
Expand All @@ -47,12 +47,21 @@ def auc(x, y, reorder=False):
Parameters
----------
x : array, shape = [n]
x coordinates.
x coordinates. These must be either monotonic increasing or monotonic
decreasing.
y : array, shape = [n]
y coordinates.
reorder : boolean, optional (default=False)
If True, assume that the curve is ascending in the case of ties, as for
an ROC curve. If the curve is non-ascending, the result will be wrong.
reorder : boolean, optional (default='deprecated')
Whether to sort x before computing. If False, assume that x must be
either monotonic increasing or monotonic decreasing. If True, y is
used to break ties when sorting x.
..deprecated:: 0.20
Parameter ``reorder`` has been deprecated in version 0.20 and will
be removed in 0.22. The result from auc will be significantly
influenced if x is sorted unexpectedly due to slight floating point
error (See issue #9786). Future (and default) behavior is equivalent
to `reorder=False`.
Returns
-------
Expand Down Expand Up @@ -83,8 +92,13 @@ def auc(x, y, reorder=False):
raise ValueError('At least 2 points are needed to compute'
' area under curve, but x.shape = %s' % x.shape)

if reorder != 'deprecated':
warnings.warn("The `reorder` parameter has been deprecated "
"in version 0.20 and will be removed in 0.22.",
DeprecationWarning)

direction = 1
if reorder:
if reorder is True:
# reorder the data points according to the x axis and using y to
# break ties
order = np.lexsort((y, x))
Expand All @@ -95,8 +109,8 @@ def auc(x, y, reorder=False):
if np.all(dx <= 0):
direction = -1
else:
raise ValueError("Reordering is not turned on, and "
"the x array is not increasing: %s" % x)
raise ValueError("x is neither increasing nor decreasing "
": {}.".format(x))

area = direction * np.trapz(y, x)
if isinstance(area, np.memmap):
Expand Down
14 changes: 13 additions & 1 deletion sklearn/metrics/tests/test_ranking.py
Expand Up @@ -20,6 +20,7 @@
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_warns
from sklearn.utils.testing import assert_warns_message

from sklearn.metrics import auc
from sklearn.metrics import average_precision_score
Expand Down Expand Up @@ -426,7 +427,18 @@ def test_auc_errors():
assert_raises(ValueError, auc, [0.0], [0.1])

# x is not in order
assert_raises(ValueError, auc, [1.0, 0.0, 0.5], [0.0, 0.0, 0.0])
x = [2, 1, 3, 4]
y = [5, 6, 7, 8]
error_message = ("x is neither increasing nor decreasing : "
"{}".format(np.array(x)))
assert_raise_message(ValueError, error_message, auc, x, y)


def test_deprecated_auc_reorder():
depr_message = ("The `reorder` parameter has been deprecated "
"in version 0.20 and will be removed in 0.22.")
assert_warns_message(DeprecationWarning, depr_message, auc,
[1, 2], [2, 3], reorder=True)


def test_auc_score_non_binary_class():
Expand Down

0 comments on commit ade8eaf

Please sign in to comment.