-
-
Notifications
You must be signed in to change notification settings - Fork 26k
FIX make it possible to specify the positive label in roc_auc_score #18107
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0d2e5dd
FIX make it possible to specify the positive label in roc_auc_score
glemaitre 67d7894
iter
glemaitre f3f2b79
Merge remote-tracking branch 'origin/master' into is/17572_alternative
glemaitre f04c7da
PEP8
glemaitre 88b7c6b
STY
glemaitre 8d4ea83
iter
glemaitre c3b5ca5
TST
glemaitre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import numbers | ||
from unittest.mock import Mock | ||
from functools import partial | ||
from _pytest.python_api import approx | ||
|
||
import numpy as np | ||
import pytest | ||
|
@@ -35,10 +36,12 @@ | |
from sklearn.datasets import make_blobs | ||
from sklearn.datasets import make_classification, make_regression | ||
from sklearn.datasets import make_multilabel_classification | ||
from sklearn.datasets import load_breast_cancer | ||
from sklearn.datasets import load_diabetes | ||
from sklearn.model_selection import train_test_split, cross_val_score | ||
from sklearn.model_selection import GridSearchCV | ||
from sklearn.multiclass import OneVsRestClassifier | ||
from sklearn.utils import shuffle | ||
|
||
|
||
REGRESSION_SCORERS = ['explained_variance', 'r2', | ||
|
@@ -747,3 +750,79 @@ def test_multiclass_roc_no_proba_scorer_errors(scorer_name): | |
msg = "'Perceptron' object has no attribute 'predict_proba'" | ||
with pytest.raises(AttributeError, match=msg): | ||
scorer(lr, X, y) | ||
|
||
|
||
def _make_imbalanced_string_dataset(): | ||
X, y = load_breast_cancer(return_X_y=True) | ||
# create an highly imbalanced | ||
idx_positive = np.flatnonzero(y == 1) | ||
idx_negative = np.flatnonzero(y == 0) | ||
idx_selected = np.hstack([idx_negative, idx_positive[:25]]) | ||
X, y = X[idx_selected], y[idx_selected] | ||
X, y = shuffle(X, y, random_state=42) | ||
# only use 2 features to make the problem even harder | ||
X = X[:, :2] | ||
y = np.array( | ||
["cancer" if c == 1 else "not cancer" for c in y], dtype=object | ||
) | ||
X_train, X_test, y_train, y_test = train_test_split( | ||
X, y, stratify=y, random_state=0, | ||
) | ||
return X_train, X_test, y_train, y_test | ||
|
||
|
||
def test_average_precision_pos_label(): | ||
from sklearn.metrics import average_precision_score | ||
X_train, X_test, y_train, y_test = _make_imbalanced_string_dataset() | ||
|
||
classifier = LogisticRegression().fit(X_train, y_train) | ||
y_proba = classifier.predict_proba(X_test) | ||
y_decision_function = classifier.decision_function(X_test) | ||
|
||
pos_label = "cancer" | ||
y_proba = y_proba[:, 0] | ||
y_decision_function *= -1 | ||
|
||
ap_proba = average_precision_score(y_test, y_proba, pos_label=pos_label) | ||
ap_decision_function = average_precision_score( | ||
y_test, y_decision_function, pos_label=pos_label | ||
) | ||
assert ap_proba == pytest.approx(ap_decision_function) | ||
|
||
average_precision_scorer = make_scorer( | ||
average_precision_score, needs_threshold=True, | ||
) | ||
with pytest.raises(ValueError): | ||
average_precision_scorer(classifier, X_test, y_test) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also check the message to make the test easier to follow. |
||
|
||
average_precision_scorer = make_scorer( | ||
average_precision_score, needs_threshold=True, pos_label=pos_label | ||
) | ||
ap_scorer = average_precision_scorer(classifier, X_test, y_test) | ||
|
||
assert ap_scorer == pytest.approx(ap_proba) | ||
|
||
|
||
def test_roc_auc_pos_label(): | ||
from sklearn.metrics import roc_auc_score | ||
X_train, X_test, y_train, y_test = _make_imbalanced_string_dataset() | ||
|
||
classifier = LogisticRegression().fit(X_train, y_train) | ||
y_proba = classifier.predict_proba(X_test) | ||
y_decision_function = classifier.decision_function(X_test) | ||
|
||
pos_label = "cancer" | ||
y_proba = y_proba[:, 0] | ||
y_decision_function *= -1 | ||
|
||
ap_proba = roc_auc_score(y_test, y_proba, pos_label=pos_label) | ||
ap_decision_function = roc_auc_score( | ||
y_test, y_decision_function, pos_label=pos_label | ||
) | ||
assert ap_proba == pytest.approx(ap_decision_function) | ||
|
||
roc_auc_scorer = make_scorer( | ||
roc_auc_score, needs_threshold=True, is_symmetric=True | ||
) | ||
ap_scorer = roc_auc_scorer(classifier, X_test, y_test) | ||
assert ap_scorer == pytest.approx(ap_proba) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a line such as:
to make the test easier to follow.