Skip to content

use pytest.approx insetad of np.array_allclose for a single float #380

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
wants to merge 1 commit into from
Closed
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
68 changes: 14 additions & 54 deletions imblearn/metrics/tests/test_score_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from imblearn.metrics import (sensitivity_score, specificity_score,
geometric_mean_score,
make_index_balanced_accuracy)

from pytest import approx

# Get the version
sk_version = sklearn.__version__
if sk_version < '0.18':
Expand All @@ -30,24 +33,18 @@ def test_imblearn_classification_scorers():
clf = LinearSVC(random_state=0)
clf.fit(X_train, y_train)

# sensitivity scorer
scorer = make_scorer(sensitivity_score, pos_label=None, average='macro')
grid = GridSearchCV(LinearSVC(random_state=0), param_grid={'C': [1, 10]},
scoring=scorer)
grid.fit(X_train, y_train).predict(X_test)
assert_allclose(grid.best_score_, 0.92, rtol=R_TOL)

scorer = make_scorer(sensitivity_score, pos_label=None, average='weighted')
grid = GridSearchCV(LinearSVC(random_state=0), param_grid={'C': [1, 10]},
scoring=scorer)
grid.fit(X_train, y_train).predict(X_test)
assert_allclose(grid.best_score_, 0.92, rtol=R_TOL)
for current_score in (sensitivity_score, specificity_score,
geometric_mean_score):
for average_value in ('macro', 'weighted', 'micro'):
scorer = make_scorer(current_score, pos_label=None,
average=average_value)
grid = GridSearchCV(LinearSVC(random_state=0),
param_grid={'C': [1, 10]},
scoring=scorer)
grid.fit(X_train, y_train).predict(X_test)
assert grid.best_score_ == approx(0.92, abs=R_TOL)

scorer = make_scorer(sensitivity_score, pos_label=None, average='micro')
grid = GridSearchCV(LinearSVC(random_state=0), param_grid={'C': [1, 10]},
scoring=scorer)
grid.fit(X_train, y_train).predict(X_test)
assert_allclose(grid.best_score_, 0.92, rtol=R_TOL)
# sensitivity scorer

scorer = make_scorer(sensitivity_score, pos_label=1)
grid = GridSearchCV(LinearSVC(random_state=0), param_grid={'C': [1, 10]},
Expand All @@ -56,50 +53,13 @@ def test_imblearn_classification_scorers():
assert_allclose(grid.best_score_, 0.92, rtol=R_TOL)

# specificity scorer
scorer = make_scorer(specificity_score, pos_label=None, average='macro')
grid = GridSearchCV(LinearSVC(random_state=0), param_grid={'C': [1, 10]},
scoring=scorer)
grid.fit(X_train, y_train).predict(X_test)
assert_allclose(grid.best_score_, 0.92, rtol=R_TOL)

scorer = make_scorer(specificity_score, pos_label=None, average='weighted')
grid = GridSearchCV(LinearSVC(random_state=0), param_grid={'C': [1, 10]},
scoring=scorer)
grid.fit(X_train, y_train).predict(X_test)
assert_allclose(grid.best_score_, 0.92, rtol=R_TOL)

scorer = make_scorer(specificity_score, pos_label=None, average='micro')
grid = GridSearchCV(LinearSVC(random_state=0), param_grid={'C': [1, 10]},
scoring=scorer)
grid.fit(X_train, y_train).predict(X_test)
assert_allclose(grid.best_score_, 0.92, rtol=R_TOL)

scorer = make_scorer(specificity_score, pos_label=1)
grid = GridSearchCV(LinearSVC(random_state=0), param_grid={'C': [1, 10]},
scoring=scorer)
grid.fit(X_train, y_train).predict(X_test)
assert_allclose(grid.best_score_, 0.95, rtol=R_TOL)

# geometric_mean scorer
scorer = make_scorer(geometric_mean_score, pos_label=None, average='macro')
grid = GridSearchCV(LinearSVC(random_state=0), param_grid={'C': [1, 10]},
scoring=scorer)
grid.fit(X_train, y_train).predict(X_test)
assert_allclose(grid.best_score_, 0.92, rtol=R_TOL)

scorer = make_scorer(
geometric_mean_score, pos_label=None, average='weighted')
grid = GridSearchCV(LinearSVC(random_state=0), param_grid={'C': [1, 10]},
scoring=scorer)
grid.fit(X_train, y_train).predict(X_test)
assert_allclose(grid.best_score_, 0.92, rtol=R_TOL)

scorer = make_scorer(geometric_mean_score, pos_label=None, average='micro')
grid = GridSearchCV(LinearSVC(random_state=0), param_grid={'C': [1, 10]},
scoring=scorer)
grid.fit(X_train, y_train).predict(X_test)
assert_allclose(grid.best_score_, 0.92, rtol=R_TOL)

scorer = make_scorer(geometric_mean_score, pos_label=1)
grid = GridSearchCV(LinearSVC(random_state=0), param_grid={'C': [1, 10]},
scoring=scorer)
Expand Down