10000 [MRG] Bugfix for precision_recall_curve when all labels are negative by varunagrawal · Pull Request #14621 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG] Bugfix for precision_recall_curve when all labels are negative #14621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sklearn/metrics/_ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ def precision_recall_curve(y_true, probas_pred, *, pos_label=None,

precision = tps / (tps + fps)
precision[np.isnan(precision)] = 0
recall = tps / tps[-1]
recall = np.ones(tps.size) if tps[-1] == 0 else tps / tps[-1]

# stop when full recall attained
# and reverse the outputs so recall is decreasing
Expand Down
37 changes: 37 additions & 0 deletions sklearn/metrics/tests/test_ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,11 @@ def test_precision_recall_curve_toydata():
precision_recall_curve(y_true, y_score)
with pytest.raises(Exception):
average_precision_score(y_true, y_score)
# p, r, _ = precision_recall_curve(y_true, y_score)
# auc_prc = average_precision_score(y_true, y_score)
# assert_array_almost_equal(p, [0, 1])
# assert_array_almost_equal(r, [1, 0.])
# assert_almost_equal(auc_prc, 0.)

y_true = [1, 1]
y_score = [0.25, 0.75]
Expand All @@ -822,6 +827,10 @@ def test_precision_recall_curve_toydata():
average_precision_score(y_true, y_score, average="macro")
with pytest.raises(Exception):
average_precision_score(y_true, y_score, average="weighted")
# assert_almost_equal(average_precision_score(y_true, y_score,
# average="macro"), 0.5)
# assert_almost_equal(average_precision_score(y_true, y_score,
# average="weighted"), 1.)
assert_almost_equal(average_precision_score(y_true, y_score,
average="samples"), 1.)
assert_almost_equal(average_precision_score(y_true, y_score,
Expand All @@ -833,6 +842,10 @@ def test_precision_recall_curve_toydata():
average_precision_score(y_true, y_score, average="macro")
with pytest.raises(Exception):
average_precision_score(y_true, y_score, average="weighted")
# assert_almost_equal(average_precision_score(y_true, y_score,
# average="macro"), 0.5)
# assert_almost_equal(average_precision_score(y_true, y_score,
# average="weighted"), 1.)
assert_almost_equal(average_precision_score(y_true, y_score,
average="samples"), 0.75)
assert_almost_equal(average_precision_score(y_true, y_score,
Expand Down Expand Up @@ -860,12 +873,36 @@ def test_precision_recall_curve_toydata():
assert_almost_equal(average_precision_score(y_true, y_score,
average="micro"), 0.5)

<<<<<<< HEAD
with np.errstate(all="ignore"):
# if one class is never present weighted should not be NaN
y_true = np.array([[0, 0], [0, 1]])
y_score = np.array([[0, 0], [0, 1]])
assert_almost_equal(average_precision_score(y_true, y_score,
average="weighted"), 1)
=======
y_true = np.array([[0, 0], [0, 0]])
y_score = np.array([[0, 1], [0, 1]])
assert_almost_equal(average_precision_score(y_true, y_score,
average="macro"), 0.)
assert_almost_equal(average_precision_score(y_true, y_score,
average="weighted"), 0.)
assert_almost_equal(average_precision_score(y_true, y_score,
average="samples"), 0.)
assert_almost_equal(average_precision_score(y_true, y_score,
average="micro"), 0.)

y_true = np.array([[1, 1], [1, 1]])
y_score = np.array([[0, 1], [0, 1]])
assert_almost_equal(average_precision_score(y_true, y_score,
average="macro"), 1.)
assert_almost_equal(average_precision_score(y_true, y_score,
average="weighted"), 1.)
assert_almost_equal(average_precision_score(y_true, y_score,
average="samples"), 1.)
assert_almost_equal(average_precision_score(y_true, y_score,
average="micro"), 1.)
>>>>>>> fixed bug for precision recall curve when all labels are negative


def test_average_precision_constant_values():
Expand Down
0