Skip to content

Commit

Permalink
Merge pull request scikit-learn#2654 from arjoly/metrics-0.15
Browse files Browse the repository at this point in the history
[MRG] Remove deprecated zero_one and zero_one_score
  • Loading branch information
GaelVaroquaux committed Dec 10, 2013
2 parents 34005e9 + c80116c commit ded381b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 105 deletions.
3 changes: 0 additions & 3 deletions sklearn/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
roc_curve,
zero_one_loss)

# Will be removed in 0.15
from .metrics import zero_one
from .metrics import zero_one_score

# Deprecated in 0.16
from .metrics import auc_score
Expand Down
65 changes: 0 additions & 65 deletions sklearn/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,48 +964,6 @@ def zero_one_loss(y_true, y_pred, normalize=True):
return n_samples - score


@deprecated("Function 'zero_one' has been renamed to "
"'zero_one_loss' and will be removed in release 0.15."
" Default behavior is changed from 'normalize=False' to "
"'normalize=True'")
def zero_one(y_true, y_pred, normalize=False):
"""Zero-One classification loss
If normalize is ``True``, return the fraction of misclassifications
(float), else it returns the number of misclassifications (int). The best
performance is 0.
Parameters
----------
y_true : array-like
y_pred : array-like
normalize : bool, optional (default=False)
If ``False`` (default), return the number of misclassifications.
Otherwise, return the fraction of misclassifications.
Returns
-------
loss : float
If normalize is True, return the fraction of misclassifications
(float), else it returns the number of misclassifications (int).
Examples
--------
>>> from sklearn.metrics import zero_one
>>> y_pred = [1, 2, 3, 4]
>>> y_true = [2, 2, 3, 4]
>>> zero_one(y_true, y_pred)
1
>>> zero_one(y_true, y_pred, normalize=True)
0.25
"""
return zero_one_loss(y_true, y_pred, normalize)


def log_loss(y_true, y_pred, eps=1e-15, normalize=True):
"""Log loss, aka logistic loss or cross-entropy loss.
Expand Down Expand Up @@ -1862,29 +1820,6 @@ def recall_score(y_true, y_pred, labels=None, pos_label=1, average='weighted'):
return r


@deprecated("Function zero_one_score has been renamed to "
'accuracy_score'" and will be removed in release 0.15.")
def zero_one_score(y_true, y_pred):
"""Zero-one classification score (accuracy)
Parameters
----------
y_true : array-like, shape = n_samples
Ground truth (correct) labels.
y_pred : array-like, shape = n_samples
Predicted labels, as returned by a classifier.
Returns
-------
score : float
Fraction of correct predictions in ``y_pred``. The best performance is
1.
"""
return accuracy_score(y_true, y_pred)


def classification_report(y_true, y_pred, labels=None, target_names=None):
"""Build a text report showing the main classification metrics
Expand Down
59 changes: 22 additions & 37 deletions sklearn/metrics/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
r2_score,
roc_auc_score,
roc_curve,
zero_one,
zero_one_score,
zero_one_loss)

from sklearn.metrics.metrics import _average_binary_score
Expand Down Expand Up @@ -370,9 +368,9 @@ def _auc(y_true, y_score):

return n_correct / float(len(pos) * len(neg))


###############################################################################
# Tests

def _average_precision(y_true, y_score):
"""Alternative implementation to check for correctness of
`average_precision_score`."""
Expand Down Expand Up @@ -496,7 +494,7 @@ def test_roc_curve_one_label():
y_true = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
y_pred = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
# assert there are warnings
w = UndefinedMetricWarning
w = UndefinedMetricWarning
fpr, tpr, thresholds = assert_warns(w, roc_curve, y_true, y_pred)
# all true labels, all fpr should be nan
assert_array_equal(fpr,
Expand Down Expand Up @@ -561,16 +559,15 @@ def test_roc_curve_toydata():
y_score = [0.25, 0.75]
tpr, fpr, _ = roc_curve(y_true, y_score)
assert_raises(ValueError, roc_auc_score, y_true, y_score)
assert_array_almost_equal(tpr, [ 0. , 0.5, 1. ])
assert_array_almost_equal(fpr, [ np.nan, np.nan, np.nan])
assert_array_almost_equal(tpr, [0., 0.5, 1.])
assert_array_almost_equal(fpr, [np.nan, np.nan, np.nan])

y_true = [1, 1]
y_score = [0.25, 0.75]
tpr, fpr, _ = roc_curve(y_true, y_score)
assert_raises(ValueError, roc_auc_score, y_true, y_score)
assert_array_almost_equal(tpr, [ np.nan, np.nan])
assert_array_almost_equal(fpr, [ 0.5, 1. ])

assert_array_almost_equal(tpr, [np.nan, np.nan])
assert_array_almost_equal(fpr, [0.5, 1.])

# Multi-label classification task
y_true = np.array([[0, 1], [0, 1]])
Expand Down Expand Up @@ -690,6 +687,7 @@ def test_auc_score_non_binary_class():
assert_raise_message(ValueError, "multiclass format is not supported",
roc_auc_score, y_true, y_pred)


def test_precision_recall_f1_score_binary():
"""Test Precision Recall and F1 Score for binary classification task"""
y_true, y_pred, _ = make_prediction(binary=True)
Expand Down Expand Up @@ -806,6 +804,7 @@ def test(y_true, y_pred):
test([str(y) for y in y_true],
[str(y) for y in y_pred])


@ignore_warnings
def test_matthews_corrcoef_nan():
assert_equal(matthews_corrcoef([0], [1]), 0.0)
Expand Down Expand Up @@ -1131,8 +1130,8 @@ def test_precision_recall_curve_toydata():
y_score = [1, 0]
p, r, _ = precision_recall_curve(y_true, y_score)
auc_prc = average_precision_score(y_true, y_score)
assert_array_almost_equal(p, [ 0.5, 0. , 1. ])
assert_array_almost_equal(r, [ 1., 0., 0.])
assert_array_almost_equal(p, [0.5, 0., 1.])
assert_array_almost_equal(r, [1., 0., 0.])
assert_almost_equal(auc_prc, 0.25)

y_true = [1, 0]
Expand Down Expand Up @@ -1168,10 +1167,9 @@ def test_precision_recall_curve_toydata():
y_score = [0.25, 0.75]
p, r, _ = precision_recall_curve(y_true, y_score)
assert_almost_equal(average_precision_score(y_true, y_score), 1.)
assert_array_almost_equal(p, [ 1. , 1., 1.])
assert_array_almost_equal(p, [1., 1., 1.])
assert_array_almost_equal(r, [1, 0.5, 0.])


# Multi-label classification task
y_true = np.array([[0, 1], [0, 1]])
y_score = np.array([[0, 1], [0, 1]])
Expand Down Expand Up @@ -1252,8 +1250,6 @@ def test_losses():
# Classification
# --------------
# Throw deprecated warning
f = ignore_warnings
assert_equal(f(zero_one)(y_true, y_pred), 11)

assert_almost_equal(zero_one_loss(y_true, y_pred),
11 / float(n_samples), 2)
Expand All @@ -1266,9 +1262,6 @@ def test_losses():
assert_equal(accuracy_score(y_true, y_pred),
1 - zero_one_loss(y_true, y_pred))

assert_equal(f(zero_one_score)(y_true, y_pred),
1 - zero_one_loss(y_true, y_pred))

# Regression
# ----------
assert_almost_equal(mean_squared_error(y_true, y_pred),
Expand Down Expand Up @@ -1327,18 +1320,6 @@ def test_symmetry():
assert_true(np.any(metric(y_true, y_pred) != metric(y_pred, y_true)),
msg="%s seems to be symmetric" % name)

# Deprecated metrics

f = ignore_warnings
assert_almost_equal(f(zero_one)(y_true, y_pred),
f(zero_one)(y_pred, y_true))

assert_almost_equal(f(zero_one)(y_true, y_pred, normalize=False),
f(zero_one)(y_pred, y_true, normalize=False))

assert_almost_equal(f(zero_one_score)(y_true, y_pred),
f(zero_one_score)(y_pred, y_true))


def test_sample_order_invariance():
y_true, y_pred, _ = make_prediction(binary=True)
Expand All @@ -1360,7 +1341,7 @@ def test_sample_order_invariance_multilabel_and_multioutput():
# Generate some data
y_true = random_state.randint(0, 2, size=(20, 25))
y_pred = random_state.randint(0, 2, size=(20, 25))
y_score =random_state.normal(size=y_true.shape)
y_score = random_state.normal(size=y_true.shape)

y_true_shuffle, y_pred_shuffle, y_score_shuffle = shuffle(y_true,
y_pred,
Expand All @@ -1381,7 +1362,6 @@ def test_sample_order_invariance_multilabel_and_multioutput():
err_msg="%s is not sample order invariant"
% name)


for name in MULTIOUTPUT_METRICS:
metric = ALL_METRICS[name]
assert_almost_equal(metric(y_true, y_score),
Expand All @@ -1393,6 +1373,7 @@ def test_sample_order_invariance_multilabel_and_multioutput():
err_msg="%s is not sample order invariant"
% name)


def test_format_invariance_with_1d_vectors():
y1, y2, _ = make_prediction(binary=True)

Expand Down Expand Up @@ -1534,6 +1515,7 @@ def check_single_sample(name):
for i, j in product([0, 1], repeat=2):
metric([i], [j])


@ignore_warnings
def check_single_sample_multioutput(name):
metric = ALL_METRICS[name]
Expand Down Expand Up @@ -1654,7 +1636,7 @@ def test_multilabel_representation_invariance():
# XXX cruel hack to work with partial functions
if isinstance(metric, partial):
metric.__module__ = 'tmp'
metric.__name__ = name
metric.__name__ = name
metric = ignore_warnings(metric)
measure = metric(y1, y2)

Expand Down Expand Up @@ -2169,7 +2151,8 @@ def test_prf_warnings():

msg = ('Recall and F-score are ill-defined and '
'being set to 0.0 in samples with no true labels.')
my_assert(w, msg, f, np.array([[1, 0], [0, 0]]), np.array([[1, 0], [1, 0]]),
my_assert(w, msg, f, np.array([[1, 0], [0, 0]]),
np.array([[1, 0], [1, 0]]),
average='samples')

# single score: micro-average
Expand All @@ -2178,8 +2161,8 @@ def test_prf_warnings():
my_assert(w, msg, f, np.array([[1, 1], [1, 1]]),
np.array([[0, 0], [0, 0]]), average='micro')

msg =('Recall and F-score are ill-defined and '
'being set to 0.0 due to no true samples.')
msg = ('Recall and F-score are ill-defined and '
'being set to 0.0 due to no true samples.')
my_assert(w, msg, f, np.array([[0, 0], [0, 0]]),
np.array([[1, 1], [1, 1]]), average='micro')

Expand Down Expand Up @@ -2490,10 +2473,12 @@ def test_averaging_multilabel_all_zeroes():

# Test _average_binary_score for weight.sum() == 0
binary_metric = (lambda y_true, y_score, average="macro":
_average_binary_score(precision_score, y_true, y_score, average))
_average_binary_score(precision_score,
y_true, y_score, average))
_check_averaging(binary_metric, y_true, y_pred, y_true_binarize,
y_pred_binarize, is_multilabel=True)


def test_averaging_multilabel_all_ones():
y_true = np.ones((20, 3))
y_pred = np.ones((20, 3))
Expand Down

0 comments on commit ded381b

Please sign in to comment.