diff --git a/imblearn/combine/tests/test_smote_enn.py b/imblearn/combine/tests/test_smote_enn.py index b7f75c6a9..284185de6 100644 --- a/imblearn/combine/tests/test_smote_enn.py +++ b/imblearn/combine/tests/test_smote_enn.py @@ -6,8 +6,8 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import (assert_allclose, assert_array_equal, - assert_raises_regex) +from sklearn.utils.testing import assert_allclose, assert_array_equal +from sklearn.utils.testing import assert_raises_regex from imblearn.combine import SMOTEENN from imblearn.under_sampling import EditedNearestNeighbours diff --git a/imblearn/combine/tests/test_smote_tomek.py b/imblearn/combine/tests/test_smote_tomek.py index a3e4e18aa..20f4b53d1 100644 --- a/imblearn/combine/tests/test_smote_tomek.py +++ b/imblearn/combine/tests/test_smote_tomek.py @@ -6,8 +6,8 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import (assert_allclose, assert_array_equal, - assert_raises_regex) +from sklearn.utils.testing import assert_allclose, assert_array_equal +from sklearn.utils.testing import assert_raises_regex from imblearn.combine import SMOTETomek from imblearn.over_sampling import SMOTE diff --git a/imblearn/datasets/tests/test_imbalance.py b/imblearn/datasets/tests/test_imbalance.py index f0cd420c1..c427a658d 100644 --- a/imblearn/datasets/tests/test_imbalance.py +++ b/imblearn/datasets/tests/test_imbalance.py @@ -11,8 +11,8 @@ import numpy as np from sklearn.datasets import load_iris -from sklearn.utils.testing import (assert_equal, assert_raises_regex, - assert_warns_message) +from sklearn.utils.testing import assert_raises_regex +from sklearn.utils.testing import assert_warns_message from imblearn.datasets import make_imbalance @@ -46,17 +46,17 @@ def test_make_imbalance_float(): X_, y_ = assert_warns_message(DeprecationWarning, "'ratio' being a float is deprecated", make_imbalance, X, Y, ratio=0.5, min_c_=1) - assert_equal(Counter(y_), {0: 50, 1: 25, 2: 50}) + assert Counter(y_) == {0: 50, 1: 25, 2: 50} # resample without using min_c_ X_, y_ = make_imbalance(X_, y_, ratio=0.25, min_c_=None) - assert_equal(Counter(y_), {0: 50, 1: 12, 2: 50}) + assert Counter(y_) == {0: 50, 1: 12, 2: 50} def test_make_imbalance_dict(): ratio = {0: 10, 1: 20, 2: 30} X_, y_ = make_imbalance(X, Y, ratio=ratio) - assert_equal(Counter(y_), ratio) + assert Counter(y_) == ratio ratio = {0: 10, 1: 20} X_, y_ = make_imbalance(X, Y, ratio=ratio) - assert_equal(Counter(y_), {0: 10, 1: 20, 2: 50}) + assert Counter(y_) == {0: 10, 1: 20, 2: 50} diff --git a/imblearn/datasets/tests/test_zenodo.py b/imblearn/datasets/tests/test_zenodo.py index aeb0948ea..2a78e2c14 100644 --- a/imblearn/datasets/tests/test_zenodo.py +++ b/imblearn/datasets/tests/test_zenodo.py @@ -7,8 +7,8 @@ # License: MIT from imblearn.datasets import fetch_datasets -from sklearn.utils.testing import (assert_equal, assert_allclose, - assert_raises_regex, SkipTest) +from sklearn.utils.testing import SkipTest, assert_allclose +from sklearn.utils.testing import assert_raises_regex DATASET_SHAPE = {'ecoli': (336, 7), 'optical_digits': (5620, 64), @@ -54,12 +54,12 @@ def test_fetch(): for k in DATASET_SHAPE.keys(): X1, X2 = datasets1[k].data, datasets2[k].data - assert_equal(DATASET_SHAPE[k], X1.shape) - assert_equal(X1.shape, X2.shape) + assert DATASET_SHAPE[k] == X1.shape + assert X1.shape == X2.shape y1, y2 = datasets1[k].target, datasets2[k].target - assert_equal((X1.shape[0],), y1.shape) - assert_equal((X1.shape[0],), y2.shape) + assert (X1.shape[0],) == y1.shape + assert (X1.shape[0],) == y2.shape def test_fetch_filter(): @@ -73,14 +73,14 @@ def test_fetch_filter(): random_state=37) X1, X2 = datasets1['ecoli'].data, datasets2['ecoli'].data - assert_equal(DATASET_SHAPE['ecoli'], X1.shape) - assert_equal(X1.shape, X2.shape) + assert DATASET_SHAPE['ecoli'] == X1.shape + assert X1.shape == X2.shape assert_allclose(X1.sum(), X2.sum()) y1, y2 = datasets1['ecoli'].target, datasets2['ecoli'].target - assert_equal((X1.shape[0],), y1.shape) - assert_equal((X1.shape[0],), y2.shape) + assert (X1.shape[0],) == y1.shape + assert (X1.shape[0],) == y2.shape def test_fetch_error(): diff --git a/imblearn/ensemble/tests/test_balance_cascade.py b/imblearn/ensemble/tests/test_balance_cascade.py index f37921ff6..dc6279c9e 100644 --- a/imblearn/ensemble/tests/test_balance_cascade.py +++ b/imblearn/ensemble/tests/test_balance_cascade.py @@ -6,8 +6,8 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import (assert_array_equal, assert_raises, - assert_raises_regex) +from sklearn.utils.testing import assert_array_equal, assert_raises +from sklearn.utils.testing import assert_raises_regex from sklearn.ensemble import RandomForestClassifier from imblearn.ensemble import BalanceCascade diff --git a/imblearn/ensemble/tests/test_easy_ensemble.py b/imblearn/ensemble/tests/test_easy_ensemble.py index 565bd7b83..7d665b1c0 100644 --- a/imblearn/ensemble/tests/test_easy_ensemble.py +++ b/imblearn/ensemble/tests/test_easy_ensemble.py @@ -6,7 +6,7 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import assert_array_equal, assert_equal +from sklearn.utils.testing import assert_array_equal from imblearn.ensemble import EasyEnsemble @@ -25,10 +25,10 @@ def test_ee_init(): ratio = 1. ee = EasyEnsemble(ratio=ratio, random_state=RND_SEED) - assert_equal(ee.ratio, ratio) - assert_equal(ee.replacement, False) - assert_equal(ee.n_subsets, 10) - assert_equal(ee.random_state, RND_SEED) + assert ee.ratio == ratio + assert ee.replacement is False + assert ee.n_subsets == 10 + assert ee.random_state == RND_SEED def test_fit_sample_auto(): diff --git a/imblearn/metrics/tests/test_classification.py b/imblearn/metrics/tests/test_classification.py index bc276cf67..41e368c05 100644 --- a/imblearn/metrics/tests/test_classification.py +++ b/imblearn/metrics/tests/test_classification.py @@ -16,15 +16,14 @@ from sklearn.preprocessing import label_binarize from sklearn.utils.fixes import np_version from sklearn.utils.validation import check_random_state -from sklearn.utils.testing import (assert_allclose, assert_array_equal, - assert_no_warnings, assert_equal, - assert_raises, assert_warns_message, - ignore_warnings, assert_not_equal, - assert_raise_message) -from sklearn.metrics import (accuracy_score, average_precision_score, - brier_score_loss, cohen_kappa_score, - jaccard_similarity_score, precision_score, - recall_score, roc_auc_score) +from sklearn.utils.testing import assert_allclose, assert_array_equal +from sklearn.utils.testing import assert_no_warnings, assert_raises +from sklearn.utils.testing import assert_warns_message, ignore_warnings +from sklearn.utils.testing import assert_raise_message +from sklearn.metrics import accuracy_score, average_precision_score +from sklearn.metrics import brier_score_loss, cohen_kappa_score +from sklearn.metrics import jaccard_similarity_score, precision_score +from sklearn.metrics import recall_score, roc_auc_score from imblearn.metrics import sensitivity_specificity_support from imblearn.metrics import sensitivity_score @@ -33,6 +32,8 @@ from imblearn.metrics import make_index_balanced_accuracy from imblearn.metrics import classification_report_imbalanced +from pytest import approx + RND_SEED = 42 R_TOL = 1e-2 @@ -113,11 +114,11 @@ def test_sensitivity_specificity_score_binary(): def test_sensitivity_specificity_f_binary_single_class(): # Such a case may occur with non-stratified cross-validation - assert_equal(1., sensitivity_score([1, 1], [1, 1])) - assert_equal(0., specificity_score([1, 1], [1, 1])) + assert sensitivity_score([1, 1], [1, 1]) == 1. + assert specificity_score([1, 1], [1, 1]) == 0. - assert_equal(0., sensitivity_score([-1, -1], [-1, -1])) - assert_equal(0., specificity_score([-1, -1], [-1, -1])) + assert sensitivity_score([-1, -1], [-1, -1]) == 0. + assert specificity_score([-1, -1], [-1, -1]) == 0. @ignore_warnings @@ -166,9 +167,8 @@ def test_sensitivity_specificity_ignored_labels(): rtol=R_TOL) # ensure the above were meaningful tests: - for average in ['macro', 'weighted', 'micro']: - assert_not_equal( - specificity_13(average=average), specificity_all(average=average)) + for each in ['macro', 'weighted', 'micro']: + assert specificity_13(average=each) != specificity_all(average=each) def test_sensitivity_specificity_error_multilabels(): @@ -333,7 +333,7 @@ def test_classification_report_imbalanced_multiclass(): y_pred, labels=np.arange(len(iris.target_names)), target_names=iris.target_names) - assert_equal(_format_report(report), expected_report) + assert _format_report(report) == expected_report # print classification report with label detection expected_report = ('pre rec spe f1 geo iba sup 0 0.83 0.79 0.92 0.81 ' '0.86 0.74 24 1 0.33 0.10 0.86 0.15 0.44 0.19 31 2 ' @@ -341,7 +341,7 @@ def test_classification_report_imbalanced_multiclass(): '0.53 0.80 0.47 0.62 0.41 75') report = classification_report_imbalanced(y_true, y_pred) - assert_equal(_format_report(report), expected_report) + assert _format_report(report) == expected_report def test_classification_report_imbalanced_multiclass_with_digits(): @@ -361,14 +361,14 @@ def test_classification_report_imbalanced_multiclass_with_digits(): labels=np.arange(len(iris.target_names)), target_names=iris.target_names, digits=5) - assert_equal(_format_report(report), expected_report) + assert _format_report(report) == expected_report # print classification report with label detection expected_report = ('pre rec spe f1 geo iba sup 0 0.83 0.79 0.92 0.81 ' '0.86 0.74 24 1 0.33 0.10 0.86 0.15 0.44 0.19 31 2 ' '0.42 0.90 0.55 0.57 0.63 0.37 20 avg / total 0.51 ' '0.53 0.80 0.47 0.62 0.41 75') report = classification_report_imbalanced(y_true, y_pred) - assert_equal(_format_report(report), expected_report) + assert _format_report(report) == expected_report def test_classification_report_imbalanced_multiclass_with_string_label(): @@ -382,7 +382,7 @@ def test_classification_report_imbalanced_multiclass_with_string_label(): '0.19 31 red 0.42 0.90 0.55 0.57 0.63 0.37 20 ' 'avg / total 0.51 0.53 0.80 0.47 0.62 0.41 75') report = classification_report_imbalanced(y_true, y_pred) - assert_equal(_format_report(report), expected_report) + assert _format_report(report) == expected_report expected_report = ('pre rec spe f1 geo iba sup a 0.83 0.79 0.92 0.81 ' '0.86 0.74 24 b 0.33 0.10 0.86 0.15 0.44 0.19 31 ' @@ -390,7 +390,7 @@ def test_classification_report_imbalanced_multiclass_with_string_label(): '0.51 0.53 0.80 0.47 0.62 0.41 75') report = classification_report_imbalanced( y_true, y_pred, target_names=["a", "b", "c"]) - assert_equal(_format_report(report), expected_report) + assert _format_report(report) == expected_report def test_classification_report_imbalanced_multiclass_with_unicode_label(): @@ -411,7 +411,7 @@ def test_classification_report_imbalanced_multiclass_with_unicode_label(): classification_report_imbalanced, y_true, y_pred) else: report = classification_report_imbalanced(y_true, y_pred) - assert_equal(_format_report(report), expected_report) + assert _format_report(report) == expected_report def test_classification_report_imbalanced_multiclass_with_long_string_label(): @@ -427,7 +427,7 @@ def test_classification_report_imbalanced_multiclass_with_long_string_label(): '0.37 20 avg / total 0.51 0.53 0.80 0.47 0.62 0.41 75') report = classification_report_imbalanced(y_true, y_pred) - assert_equal(_format_report(report), expected_report) + assert _format_report(report) == expected_report def test_iba_sklearn_metrics(): @@ -436,22 +436,22 @@ def test_iba_sklearn_metrics(): acc = make_index_balanced_accuracy(alpha=0.5, squared=True)( accuracy_score) score = acc(y_true, y_pred) - assert_equal(score, 0.54756) + assert score == approx(0.54756) jss = make_index_balanced_accuracy(alpha=0.5, squared=True)( jaccard_similarity_score) score = jss(y_true, y_pred) - assert_equal(score, 0.54756) + assert score == approx(0.54756) pre = make_index_balanced_accuracy(alpha=0.5, squared=True)( precision_score) score = pre(y_true, y_pred) - assert_equal(score, 0.65025) + assert score == approx(0.65025) rec = make_index_balanced_accuracy(alpha=0.5, squared=True)( recall_score) score = rec(y_true, y_pred) - assert_equal(score, 0.41616000000000009) + assert score == approx(0.41616000000000009) def test_iba_error_y_score_prob(): diff --git a/imblearn/over_sampling/tests/test_adasyn.py b/imblearn/over_sampling/tests/test_adasyn.py index eb68dd06c..c4b8823a6 100644 --- a/imblearn/over_sampling/tests/test_adasyn.py +++ b/imblearn/over_sampling/tests/test_adasyn.py @@ -6,8 +6,8 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import (assert_allclose, assert_array_equal, - assert_equal, assert_raises_regex) +from sklearn.utils.testing import assert_allclose, assert_array_equal +from sklearn.utils.testing import assert_raises_regex from sklearn.neighbors import NearestNeighbors from imblearn.over_sampling import ADASYN @@ -30,13 +30,13 @@ def test_ada_init(): ratio = 'auto' ada = ADASYN(ratio=ratio, random_state=RND_SEED) - assert_equal(ada.random_state, RND_SEED) + assert ada.random_state == RND_SEED def test_ada_fit(): ada = ADASYN(random_state=RND_SEED) ada.fit(X, Y) - assert_equal(ada.ratio_, {0: 4, 1: 0}) + assert ada.ratio_ == {0: 4, 1: 0} def test_ada_fit_sample(): diff --git a/imblearn/over_sampling/tests/test_random_over_sampler.py b/imblearn/over_sampling/tests/test_random_over_sampler.py index 40479421b..1e58a1057 100644 --- a/imblearn/over_sampling/tests/test_random_over_sampler.py +++ b/imblearn/over_sampling/tests/test_random_over_sampler.py @@ -8,7 +8,7 @@ from collections import Counter import numpy as np -from sklearn.utils.testing import assert_array_equal, assert_equal +from sklearn.utils.testing import assert_array_equal from imblearn.over_sampling import RandomOverSampler @@ -24,7 +24,7 @@ def test_ros_init(): ratio = 'auto' ros = RandomOverSampler(ratio=ratio, random_state=RND_SEED) - assert_equal(ros.random_state, RND_SEED) + assert ros.random_state == RND_SEED def test_ros_fit_sample(): @@ -75,6 +75,6 @@ def test_multiclass_fit_sample(): ros = RandomOverSampler(random_state=RND_SEED) X_resampled, y_resampled = ros.fit_sample(X, y) count_y_res = Counter(y_resampled) - assert_equal(count_y_res[0], 5) - assert_equal(count_y_res[1], 5) - assert_equal(count_y_res[2], 5) + assert count_y_res[0] == 5 + assert count_y_res[1] == 5 + assert count_y_res[2] == 5 diff --git a/imblearn/over_sampling/tests/test_smote.py b/imblearn/over_sampling/tests/test_smote.py index 3b719ffd7..90dfcc836 100644 --- a/imblearn/over_sampling/tests/test_smote.py +++ b/imblearn/over_sampling/tests/test_smote.py @@ -6,8 +6,8 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import (assert_allclose, assert_array_equal, - assert_raises_regex) +from sklearn.utils.testing import assert_allclose, assert_array_equal +from sklearn.utils.testing import assert_raises_regex from sklearn.neighbors import NearestNeighbors from sklearn.svm import SVC diff --git a/imblearn/tests/test_common.py b/imblearn/tests/test_common.py index 7670a3f19..aab754c3c 100644 --- a/imblearn/tests/test_common.py +++ b/imblearn/tests/test_common.py @@ -3,8 +3,6 @@ # Christos Aridas # License: MIT -from sklearn.utils.testing import assert_greater -from sklearn.utils.testing import assert_false from sklearn.utils.testing import _named_check from imblearn.utils.estimator_checks import check_estimator, _yield_all_checks @@ -16,12 +14,12 @@ def test_all_estimator_no_base_class(): for name, Estimator in all_estimators(): msg = ("Base estimators such as {0} should not be included" " in all_estimators").format(name) - assert_false(name.lower().startswith('base'), msg=msg) + assert not name.lower().startswith('base'), msg def test_all_estimators(): estimators = all_estimators(include_meta_estimators=True) - assert_greater(len(estimators), 0) + assert len(estimators) > 0 for name, Estimator in estimators: # some can just not be sensibly default constructed yield (_named_check(check_estimator, name), diff --git a/imblearn/tests/test_pipeline.py b/imblearn/tests/test_pipeline.py index 4a67e112b..8c581781f 100644 --- a/imblearn/tests/test_pipeline.py +++ b/imblearn/tests/test_pipeline.py @@ -14,12 +14,8 @@ from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_raises_regex from sklearn.utils.testing import assert_raise_message -from sklearn.utils.testing import assert_equal -from sklearn.utils.testing import assert_false -from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_array_almost_equal -from sklearn.utils.testing import assert_dict_equal from sklearn.utils.testing import assert_allclose from sklearn.base import clone, BaseEstimator @@ -190,14 +186,14 @@ def test_pipeline_init(): # Smoke test with only an estimator clf = NoTrans() pipe = Pipeline([('svc', clf)]) - assert_equal(pipe.get_params(deep=True), - dict(svc__a=None, svc__b=None, svc=clf, - **pipe.get_params(deep=False))) + expected = dict(svc__a=None, svc__b=None, svc=clf, + **pipe.get_params(deep=False)) + assert pipe.get_params(deep=True) == expected # Check that params are set pipe.set_params(svc__a=0.1) - assert_equal(clf.a, 0.1) - assert_equal(clf.b, None) + assert clf.a == 0.1 + assert clf.b is None # Smoke test the repr: repr(pipe) @@ -214,7 +210,7 @@ def test_pipeline_init(): # Check that params are set pipe.set_params(svc__C=0.1) - assert_equal(clf.C, 0.1) + assert clf.C == 0.1 # Smoke test the repr: repr(pipe) @@ -223,7 +219,7 @@ def test_pipeline_init(): # Test clone pipe2 = clone(pipe) - assert_false(pipe.named_steps['svc'] is pipe2.named_steps['svc']) + assert not pipe.named_steps['svc'] is pipe2.named_steps['svc'] # Check that apart from estimators, the parameters are the same params = pipe.get_params(deep=True) @@ -240,7 +236,7 @@ def test_pipeline_init(): params.pop('anova') params2.pop('svc') params2.pop('anova') - assert_equal(params, params2) + assert params == params2 def test_pipeline_methods_anova(): @@ -264,10 +260,10 @@ def test_pipeline_fit_params(): pipe = Pipeline([('transf', Transf()), ('clf', FitParamT())]) pipe.fit(X=None, y=None, clf__should_succeed=True) # classifier should return True - assert_true(pipe.predict(None)) + assert pipe.predict(None) # and transformer params should not be changed - assert_true(pipe.named_steps['transf'].a is None) - assert_true(pipe.named_steps['transf'].b is None) + assert pipe.named_steps['transf'].a is None + assert pipe.named_steps['transf'].b is None # invalid parameters should raise an error message assert_raise_message( TypeError, @@ -281,10 +277,10 @@ def test_pipeline_sample_weight_supported(): X = np.array([[1, 2]]) pipe = Pipeline([('transf', Transf()), ('clf', FitParamT())]) pipe.fit(X, y=None) - assert_equal(pipe.score(X), 3) - assert_equal(pipe.score(X, y=None), 3) - assert_equal(pipe.score(X, y=None, sample_weight=None), 3) - assert_equal(pipe.score(X, sample_weight=np.array([2, 3])), 8) + assert pipe.score(X) == 3 + assert pipe.score(X, y=None) == 3 + assert pipe.score(X, y=None, sample_weight=None) == 3 + assert pipe.score(X, sample_weight=np.array([2, 3])) == 8 def test_pipeline_sample_weight_unsupported(): @@ -292,8 +288,8 @@ def test_pipeline_sample_weight_unsupported(): X = np.array([[1, 2]]) pipe = Pipeline([('transf', Transf()), ('clf', Mult())]) pipe.fit(X, y=None) - assert_equal(pipe.score(X), 3) - assert_equal(pipe.score(X, sample_weight=None), 3) + assert pipe.score(X) == 3 + assert pipe.score(X, sample_weight=None) == 3 assert_raise_message( TypeError, "score() got an unexpected keyword argument 'sample_weight'", @@ -355,16 +351,16 @@ def test_pipeline_methods_preprocessing_svm(): # check shapes of various prediction functions predict = pipe.predict(X) - assert_equal(predict.shape, (n_samples,)) + assert predict.shape == (n_samples,) proba = pipe.predict_proba(X) - assert_equal(proba.shape, (n_samples, n_classes)) + assert proba.shape == (n_samples, n_classes) log_proba = pipe.predict_log_proba(X) - assert_equal(log_proba.shape, (n_samples, n_classes)) + assert log_proba.shape == (n_samples, n_classes) decision_function = pipe.decision_function(X) - assert_equal(decision_function.shape, (n_samples, n_classes)) + assert decision_function.shape == (n_samples, n_classes) pipe.score(X, y) @@ -414,9 +410,9 @@ def test_fit_predict_with_intermediate_fit_params(): y=None, transf__should_get_this=True, clf__should_succeed=True) - assert_true(pipe.named_steps['transf'].fit_params['should_get_this']) - assert_true(pipe.named_steps['clf'].successful) - assert_false('should_succeed' in pipe.named_steps['transf'].fit_params) + assert pipe.named_steps['transf'].fit_params['should_get_this'] + assert pipe.named_steps['clf'].successful + assert 'should_succeed' not in pipe.named_steps['transf'].fit_params def test_pipeline_transform(): @@ -457,21 +453,21 @@ def test_set_pipeline_steps(): transf1 = Transf() transf2 = Transf() pipeline = Pipeline([('mock', transf1)]) - assert_true(pipeline.named_steps['mock'] is transf1) + assert pipeline.named_steps['mock'] is transf1 # Directly setting attr pipeline.steps = [('mock2', transf2)] - assert_true('mock' not in pipeline.named_steps) - assert_true(pipeline.named_steps['mock2'] is transf2) - assert_equal([('mock2', transf2)], pipeline.steps) + assert 'mock' not in pipeline.named_steps + assert pipeline.named_steps['mock2'] is transf2 + assert [('mock2', transf2)] == pipeline.steps # Using set_params pipeline.set_params(steps=[('mock', transf1)]) - assert_equal([('mock', transf1)], pipeline.steps) + assert [('mock', transf1)] == pipeline.steps # Using set_params to replace single step pipeline.set_params(mock=transf2) - assert_equal([('mock', transf2)], pipeline.steps) + assert [('mock', transf2)] == pipeline.steps # With invalid data pipeline.set_params(steps=[('junk', ())]) @@ -502,15 +498,14 @@ def make(): assert_array_equal([[exp]], pipeline.fit_transform(X, y)) assert_array_equal([exp], pipeline.fit(X).predict(X)) assert_array_equal(X, pipeline.inverse_transform([[exp]])) - assert_dict_equal(pipeline.get_params(deep=True), - {'steps': pipeline.steps, + expected_params = {'steps': pipeline.steps, 'm2': mult2, 'm3': None, 'last': mult5, 'memory': None, 'm2__mult': 2, - 'last__mult': 5, - }) + 'last__mult': 5} + assert pipeline.get_params(deep=True) == expected_params pipeline.set_params(m2=None) exp = 5 @@ -558,39 +553,39 @@ def test_pipeline_ducktyping(): pipeline.inverse_transform pipeline = make_pipeline(Transf()) - assert_false(hasattr(pipeline, 'predict')) + assert not hasattr(pipeline, 'predict') pipeline.transform pipeline.inverse_transform pipeline = make_pipeline(None) - assert_false(hasattr(pipeline, 'predict')) + assert not hasattr(pipeline, 'predict') pipeline.transform pipeline.inverse_transform pipeline = make_pipeline(Transf(), NoInvTransf()) - assert_false(hasattr(pipeline, 'predict')) + assert not hasattr(pipeline, 'predict') pipeline.transform - assert_false(hasattr(pipeline, 'inverse_transform')) + assert not hasattr(pipeline, 'inverse_transform') pipeline = make_pipeline(NoInvTransf(), Transf()) - assert_false(hasattr(pipeline, 'predict')) + assert not hasattr(pipeline, 'predict') pipeline.transform - assert_false(hasattr(pipeline, 'inverse_transform')) + assert not hasattr(pipeline, 'inverse_transform') def test_make_pipeline(): t1 = Transf() t2 = Transf() pipe = make_pipeline(t1, t2) - assert_true(isinstance(pipe, Pipeline)) - assert_equal(pipe.steps[0][0], "transf-1") - assert_equal(pipe.steps[1][0], "transf-2") + assert isinstance(pipe, Pipeline) + assert pipe.steps[0][0] == "transf-1" + assert pipe.steps[1][0] == "transf-2" pipe = make_pipeline(t1, t2, FitParamT()) - assert_true(isinstance(pipe, Pipeline)) - assert_equal(pipe.steps[0][0], "transf-1") - assert_equal(pipe.steps[1][0], "transf-2") - assert_equal(pipe.steps[2][0], "fitparamt") + assert isinstance(pipe, Pipeline) + assert pipe.steps[0][0] == "transf-1" + assert pipe.steps[1][0] == "transf-2" + assert pipe.steps[2][0] == "fitparamt" def test_classes_property(): @@ -641,7 +636,7 @@ def test_pipeline_memory_transformer(): cached_pipe.fit(X, y) pipe.fit(X, y) # Get the time stamp of the tranformer in the cached pipeline - ts = cached_pipe.named_steps['transf'].timestamp_ + expected_ts = cached_pipe.named_steps['transf'].timestamp_ # Check that cached_pipe and pipe yield identical results assert_array_equal(pipe.predict(X), cached_pipe.predict(X)) assert_array_equal(pipe.predict_proba(X), cached_pipe.predict_proba(X)) @@ -650,7 +645,7 @@ def test_pipeline_memory_transformer(): assert_array_equal(pipe.score(X, y), cached_pipe.score(X, y)) assert_array_equal(pipe.named_steps['transf'].means_, cached_pipe.named_steps['transf'].means_) - assert_false(hasattr(transf, 'means_')) + assert not hasattr(transf, 'means_') # Check that we are reading the cache while fitting # a second time cached_pipe.fit(X, y) @@ -662,7 +657,7 @@ def test_pipeline_memory_transformer(): assert_array_equal(pipe.score(X, y), cached_pipe.score(X, y)) assert_array_equal(pipe.named_steps['transf'].means_, cached_pipe.named_steps['transf'].means_) - assert_equal(ts, cached_pipe.named_steps['transf'].timestamp_) + assert cached_pipe.named_steps['transf'].timestamp_ == expected_ts # Create a new pipeline with cloned estimators # Check that even changing the name step does not affect the cache hit clf_2 = SVC(probability=True, random_state=0) @@ -680,7 +675,7 @@ def test_pipeline_memory_transformer(): assert_array_equal(pipe.score(X, y), cached_pipe_2.score(X, y)) assert_array_equal(pipe.named_steps['transf'].means_, cached_pipe_2.named_steps['transf_2'].means_) - assert_equal(ts, cached_pipe_2.named_steps['transf_2'].timestamp_) + assert cached_pipe_2.named_steps['transf_2'].timestamp_ == expected_ts finally: shutil.rmtree(cachedir) @@ -711,7 +706,7 @@ def test_pipeline_memory_sampler(): cached_pipe.fit(X, y) pipe.fit(X, y) # Get the time stamp of the tranformer in the cached pipeline - ts = cached_pipe.named_steps['transf'].timestamp_ + expected_ts = cached_pipe.named_steps['transf'].timestamp_ # Check that cached_pipe and pipe yield identical results assert_array_equal(pipe.predict(X), cached_pipe.predict(X)) assert_array_equal(pipe.predict_proba(X), cached_pipe.predict_proba(X)) @@ -720,7 +715,7 @@ def test_pipeline_memory_sampler(): assert_array_equal(pipe.score(X, y), cached_pipe.score(X, y)) assert_array_equal(pipe.named_steps['transf'].means_, cached_pipe.named_steps['transf'].means_) - assert_false(hasattr(transf, 'means_')) + assert not hasattr(transf, 'means_') # Check that we are reading the cache while fitting # a second time cached_pipe.fit(X, y) @@ -732,7 +727,7 @@ def test_pipeline_memory_sampler(): assert_array_equal(pipe.score(X, y), cached_pipe.score(X, y)) assert_array_equal(pipe.named_steps['transf'].means_, cached_pipe.named_steps['transf'].means_) - assert_equal(ts, cached_pipe.named_steps['transf'].timestamp_) + assert cached_pipe.named_steps['transf'].timestamp_ == expected_ts # Create a new pipeline with cloned estimators # Check that even changing the name step does not affect the cache hit clf_2 = SVC(probability=True, random_state=0) @@ -750,7 +745,7 @@ def test_pipeline_memory_sampler(): assert_array_equal(pipe.score(X, y), cached_pipe_2.score(X, y)) assert_array_equal(pipe.named_steps['transf'].means_, cached_pipe_2.named_steps['transf_2'].means_) - assert_equal(ts, cached_pipe_2.named_steps['transf_2'].timestamp_) + assert cached_pipe_2.named_steps['transf_2'].timestamp_ == expected_ts finally: shutil.rmtree(cachedir) diff --git a/imblearn/under_sampling/prototype_generation/tests/test_cluster_centroids.py b/imblearn/under_sampling/prototype_generation/tests/test_cluster_centroids.py index f3d73e67a..a42cb2e19 100644 --- a/imblearn/under_sampling/prototype_generation/tests/test_cluster_centroids.py +++ b/imblearn/under_sampling/prototype_generation/tests/test_cluster_centroids.py @@ -4,8 +4,9 @@ from collections import Counter import numpy as np -from sklearn.utils.testing import (assert_allclose, assert_array_equal, - assert_equal, assert_raises_regex) +from sklearn.utils.testing import assert_allclose +from sklearn.utils.testing import assert_array_equal +from sklearn.utils.testing import assert_raises_regex from sklearn.cluster import KMeans from imblearn.under_sampling import ClusterCentroids @@ -53,9 +54,9 @@ def test_multiclass_fit_sample(): cc = ClusterCentroids(random_state=RND_SEED) X_resampled, y_resampled = cc.fit_sample(X, y) count_y_res = Counter(y_resampled) - assert_equal(count_y_res[0], 2) - assert_equal(count_y_res[1], 2) - assert_equal(count_y_res[2], 2) + assert count_y_res[0] == 2 + assert count_y_res[1] == 2 + assert count_y_res[2] == 2 def test_fit_sample_object(): diff --git a/imblearn/under_sampling/prototype_selection/tests/test_allknn.py b/imblearn/under_sampling/prototype_selection/tests/test_allknn.py index 7e1c92738..8208104c3 100644 --- a/imblearn/under_sampling/prototype_selection/tests/test_allknn.py +++ b/imblearn/under_sampling/prototype_selection/tests/test_allknn.py @@ -6,8 +6,8 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import (assert_allclose, assert_array_equal, - assert_raises, assert_true) +from sklearn.utils.testing import assert_allclose, assert_array_equal +from sklearn.utils.testing import assert_raises from sklearn.neighbors import NearestNeighbors from sklearn.datasets import make_classification @@ -77,7 +77,7 @@ def test_all_knn_allow_minority(): X_res_1, y_res_1 = allknn.fit_sample(X, y) allknn = AllKNN(random_state=RND_SEED) X_res_2, y_res_2 = allknn.fit_sample(X, y) - assert_true(len(y_res_1) < len(y_res_2)) + assert len(y_res_1) < len(y_res_2) def test_allknn_fit_sample_with_indices(): diff --git a/imblearn/under_sampling/prototype_selection/tests/test_condensed_nearest_neighbour.py b/imblearn/under_sampling/prototype_selection/tests/test_condensed_nearest_neighbour.py index 247ee5109..360422c70 100644 --- a/imblearn/under_sampling/prototype_selection/tests/test_condensed_nearest_neighbour.py +++ b/imblearn/under_sampling/prototype_selection/tests/test_condensed_nearest_neighbour.py @@ -6,8 +6,8 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import (assert_array_equal, assert_equal, - assert_raises_regex) +from sklearn.utils.testing import assert_array_equal +from sklearn.utils.testing import assert_raises_regex from sklearn.neighbors import KNeighborsClassifier @@ -30,8 +30,8 @@ def test_cnn_init(): cnn = CondensedNearestNeighbour(random_state=RND_SEED) - assert_equal(cnn.n_seeds_S, 1) - assert_equal(cnn.n_jobs, 1) + assert cnn.n_seeds_S == 1 + assert cnn.n_jobs == 1 def test_cnn_fit_sample(): diff --git a/imblearn/under_sampling/prototype_selection/tests/test_edited_nearest_neighbours.py b/imblearn/under_sampling/prototype_selection/tests/test_edited_nearest_neighbours.py index d77255cdd..6266840d1 100644 --- a/imblearn/under_sampling/prototype_selection/tests/test_edited_nearest_neighbours.py +++ b/imblearn/under_sampling/prototype_selection/tests/test_edited_nearest_neighbours.py @@ -6,8 +6,8 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import (assert_array_equal, assert_equal, - assert_raises_regex) +from sklearn.utils.testing import assert_array_equal +from sklearn.utils.testing import assert_raises_regex from sklearn.neighbors import NearestNeighbors @@ -30,10 +30,10 @@ def test_enn_init(): enn = EditedNearestNeighbours(random_state=RND_SEED) - assert_equal(enn.n_neighbors, 3) - assert_equal(enn.kind_sel, 'all') - assert_equal(enn.n_jobs, 1) - assert_equal(enn.random_state, RND_SEED) + assert enn.n_neighbors == 3 + assert enn.kind_sel == 'all' + assert enn.n_jobs == 1 + assert enn.random_state == RND_SEED def test_enn_fit_sample(): diff --git a/imblearn/under_sampling/prototype_selection/tests/test_instance_hardness_threshold.py b/imblearn/under_sampling/prototype_selection/tests/test_instance_hardness_threshold.py index 95a39f0a0..528ec8a4a 100644 --- a/imblearn/under_sampling/prototype_selection/tests/test_instance_hardness_threshold.py +++ b/imblearn/under_sampling/prototype_selection/tests/test_instance_hardness_threshold.py @@ -6,8 +6,9 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import (assert_array_equal, assert_equal, - assert_raises, assert_raises_regex) +from sklearn.utils.testing import assert_array_equal +from sklearn.utils.testing import assert_raises +from sklearn.utils.testing import assert_raises_regex from sklearn.ensemble import GradientBoostingClassifier from imblearn.under_sampling import InstanceHardnessThreshold @@ -38,8 +39,8 @@ def test_iht_init(): iht = InstanceHardnessThreshold( ESTIMATOR, ratio=ratio, random_state=RND_SEED) - assert_equal(iht.ratio, ratio) - assert_equal(iht.random_state, RND_SEED) + assert iht.ratio == ratio + assert iht.random_state == RND_SEED def test_iht_fit_sample(): diff --git a/imblearn/under_sampling/prototype_selection/tests/test_nearmiss.py b/imblearn/under_sampling/prototype_selection/tests/test_nearmiss.py index 256f2995e..9424e94be 100644 --- a/imblearn/under_sampling/prototype_selection/tests/test_nearmiss.py +++ b/imblearn/under_sampling/prototype_selection/tests/test_nearmiss.py @@ -6,8 +6,8 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import (assert_array_equal, assert_warns, - assert_raises_regex) +from sklearn.utils.testing import assert_array_equal, assert_warns +from sklearn.utils.testing import assert_raises_regex from sklearn.neighbors import NearestNeighbors from imblearn.under_sampling import NearMiss diff --git a/imblearn/under_sampling/prototype_selection/tests/test_one_sided_selection.py b/imblearn/under_sampling/prototype_selection/tests/test_one_sided_selection.py index b88d3df10..2ae1eadd4 100644 --- a/imblearn/under_sampling/prototype_selection/tests/test_one_sided_selection.py +++ b/imblearn/under_sampling/prototype_selection/tests/test_one_sided_selection.py @@ -6,8 +6,8 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import (assert_array_equal, assert_equal, - assert_raises_regex) +from sklearn.utils.testing import assert_array_equal +from sklearn.utils.testing import assert_raises_regex from sklearn.neighbors import KNeighborsClassifier @@ -28,9 +28,9 @@ def test_oss_init(): oss = OneSidedSelection(random_state=RND_SEED) - assert_equal(oss.n_seeds_S, 1) - assert_equal(oss.n_jobs, 1) - assert_equal(oss.random_state, RND_SEED) + assert oss.n_seeds_S == 1 + assert oss.n_jobs == 1 + assert oss.random_state == RND_SEED def test_oss_fit_sample(): diff --git a/imblearn/under_sampling/prototype_selection/tests/test_random_under_sampler.py b/imblearn/under_sampling/prototype_selection/tests/test_random_under_sampler.py index 24e11c65b..4b2f5fae6 100644 --- a/imblearn/under_sampling/prototype_selection/tests/test_random_under_sampler.py +++ b/imblearn/under_sampling/prototype_selection/tests/test_random_under_sampler.py @@ -8,7 +8,7 @@ from collections import Counter import numpy as np -from sklearn.utils.testing import assert_array_equal, assert_equal +from sklearn.utils.testing import assert_array_equal from imblearn.under_sampling import RandomUnderSampler @@ -73,6 +73,6 @@ def test_multiclass_fit_sample(): rus = RandomUnderSampler(random_state=RND_SEED) X_resampled, y_resampled = rus.fit_sample(X, y) count_y_res = Counter(y_resampled) - assert_equal(count_y_res[0], 2) - assert_equal(count_y_res[1], 2) - assert_equal(count_y_res[2], 2) + assert count_y_res[0] == 2 + assert count_y_res[1] == 2 + assert count_y_res[2] == 2 diff --git a/imblearn/under_sampling/prototype_selection/tests/test_repeated_edited_nearest_neighbours.py b/imblearn/under_sampling/prototype_selection/tests/test_repeated_edited_nearest_neighbours.py index c5bc63170..af0ff9c2e 100644 --- a/imblearn/under_sampling/prototype_selection/tests/test_repeated_edited_nearest_neighbours.py +++ b/imblearn/under_sampling/prototype_selection/tests/test_repeated_edited_nearest_neighbours.py @@ -6,8 +6,8 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import (assert_array_equal, assert_equal, - assert_raises) +from sklearn.utils.testing import assert_array_equal +from sklearn.utils.testing import assert_raises from sklearn.neighbors import NearestNeighbors @@ -43,10 +43,10 @@ def test_renn_init(): renn = RepeatedEditedNearestNeighbours(random_state=RND_SEED) - assert_equal(renn.n_neighbors, 3) - assert_equal(renn.kind_sel, 'all') - assert_equal(renn.n_jobs, 1) - assert_equal(renn.random_state, RND_SEED) + assert renn.n_neighbors == 3 + assert renn.kind_sel == 'all' + assert renn.n_jobs == 1 + assert renn.random_state == RND_SEED def test_renn_iter_wrong(): diff --git a/imblearn/under_sampling/prototype_selection/tests/test_tomek_links.py b/imblearn/under_sampling/prototype_selection/tests/test_tomek_links.py index 743fe91ef..300735b79 100644 --- a/imblearn/under_sampling/prototype_selection/tests/test_tomek_links.py +++ b/imblearn/under_sampling/prototype_selection/tests/test_tomek_links.py @@ -6,7 +6,7 @@ from __future__ import print_function import numpy as np -from sklearn.utils.testing import assert_array_equal, assert_equal +from sklearn.utils.testing import assert_array_equal from imblearn.under_sampling import TomekLinks @@ -27,8 +27,8 @@ def test_tl_init(): tl = TomekLinks(random_state=RND_SEED) - assert_equal(tl.n_jobs, 1) - assert_equal(tl.random_state, RND_SEED) + assert tl.n_jobs == 1 + assert tl.random_state == RND_SEED def test_tl_fit_sample(): diff --git a/imblearn/utils/estimator_checks.py b/imblearn/utils/estimator_checks.py index cbc223f13..9f7dd48a0 100644 --- a/imblearn/utils/estimator_checks.py +++ b/imblearn/utils/estimator_checks.py @@ -18,9 +18,8 @@ as sklearn_yield_all_checks, check_estimator \ as sklearn_check_estimator, check_parameters_default_constructible from sklearn.exceptions import NotFittedError -from sklearn.utils.testing import (assert_warns, assert_raises_regex, - assert_true, set_random_state, - assert_equal) +from sklearn.utils.testing import assert_warns, assert_raises_regex +from sklearn.utils.testing import set_random_state from imblearn.base import SamplerMixin from imblearn.over_sampling.base import BaseOverSampler @@ -128,24 +127,23 @@ def check_dont_overwrite_parameters(name, Estimator): if key not in dict_before_fit.keys()] # check that fit doesn't add any public attribute - assert_true(not attrs_added_by_fit, - ('Estimator adds public attribute(s) during' - ' the fit method.' - ' Estimators are only allowed to add private attributes' - ' either started with _ or ended' - ' with _ but %s added' % ', '.join(attrs_added_by_fit))) + assert not attrs_added_by_fit, ('Estimator adds public attribute(s) during' + ' the fit method. Estimators are only' + ' allowed to add private attributes either' + ' started with _ or ended with _ but %s' + ' added' % ', '.join(attrs_added_by_fit)) # check that fit doesn't change any public attribute attrs_changed_by_fit = [key for key in public_keys_after_fit if (dict_before_fit[key] is not dict_after_fit[key])] - assert_true(not attrs_changed_by_fit, - ('Estimator changes public attribute(s) during' - ' the fit method. Estimators are only allowed' - ' to change attributes started' - ' or ended with _, but' - ' %s changed' % ', '.join(attrs_changed_by_fit))) + assert not attrs_changed_by_fit, ('Estimator changes public attribute(s)' + ' during the fit method. Estimators are' + ' only allowed to change attributes' + ' started or ended with _, but %s' + ' changed' % + ', '.join(attrs_changed_by_fit)) def check_samplers_one_label(name, Sampler): @@ -192,7 +190,7 @@ def check_samplers_fit(name, Sampler): X = np.random.random((30, 2)) y = np.array([1] * 20 + [0] * 10) sampler.fit(X, y) - assert_true(hasattr(sampler, 'ratio_')) + assert hasattr(sampler, 'ratio_') def check_samplers_fit_sample(name, Sampler): @@ -205,24 +203,21 @@ def check_samplers_fit_sample(name, Sampler): if isinstance(sampler, BaseOverSampler): target_stats_res = Counter(y_res) n_samples = max(target_stats.values()) - assert_true(all(value >= n_samples - for value in Counter(y_res).values())) + assert all(value >= n_samples for value in Counter(y_res).values()) elif isinstance(sampler, BaseUnderSampler): n_samples = min(target_stats.values()) - assert_true(all(value == n_samples - for value in Counter(y_res).values())) + assert all(value == n_samples for value in Counter(y_res).values()) elif isinstance(sampler, BaseCleaningSampler): target_stats_res = Counter(y_res) class_minority = min(target_stats, key=target_stats.get) - assert_true( - all(target_stats[class_sample] > target_stats_res[class_sample] - for class_sample in target_stats.keys() - if class_sample != class_minority)) + assert all(target_stats[class_sample] > target_stats_res[class_sample] + for class_sample in target_stats.keys() + if class_sample != class_minority) elif isinstance(sampler, BaseEnsembleSampler): y_ensemble = y_res[0] n_samples = min(target_stats.values()) - assert_true(all(value == n_samples - for value in Counter(y_ensemble).values())) + assert all(value == n_samples + for value in Counter(y_ensemble).values()) def check_samplers_ratio_fit_sample(name, Sampler): @@ -230,26 +225,26 @@ def check_samplers_ratio_fit_sample(name, Sampler): X, y = make_classification(n_samples=1000, n_classes=3, n_informative=4, weights=[0.2, 0.3, 0.5], random_state=0) - target_stats = Counter(y) sampler = Sampler(random_state=0) + expected_stat = Counter(y)[1] if isinstance(sampler, BaseOverSampler): ratio = {2: 498, 0: 498} sampler.set_params(ratio=ratio) X_res, y_res = sampler.fit_sample(X, y) - assert_equal(target_stats[1], Counter(y_res)[1]) + assert Counter(y_res)[1] == expected_stat elif isinstance(sampler, BaseUnderSampler): ratio = {2: 201, 0: 201} sampler.set_params(ratio=ratio) X_res, y_res = sampler.fit_sample(X, y) - assert_equal(target_stats[1], Counter(y_res)[1]) + assert Counter(y_res)[1] == expected_stat elif isinstance(sampler, BaseCleaningSampler): ratio = {2: 201, 0: 201} sampler.set_params(ratio=ratio) X_res, y_res = sampler.fit_sample(X, y) - assert_equal(target_stats[1], Counter(y_res)[1]) + assert Counter(y_res)[1] == expected_stat elif isinstance(sampler, BaseEnsembleSampler): ratio = {2: 201, 0: 201} sampler.set_params(ratio=ratio) X_res, y_res = sampler.fit_sample(X, y) y_ensemble = y_res[0] - assert_equal(target_stats[1], Counter(y_ensemble)[1]) + assert Counter(y_ensemble)[1] == expected_stat diff --git a/imblearn/utils/tests/test_estimator_checks.py b/imblearn/utils/tests/test_estimator_checks.py index 599039795..f423929f5 100644 --- a/imblearn/utils/tests/test_estimator_checks.py +++ b/imblearn/utils/tests/test_estimator_checks.py @@ -5,7 +5,7 @@ from sklearn.externals.six.moves import cStringIO as StringIO from sklearn.base import BaseEstimator, ClassifierMixin -from sklearn.utils.testing import assert_raises_regex, assert_true +from sklearn.utils.testing import assert_raises_regex from sklearn.utils.validation import check_X_y, check_array from imblearn.utils.estimator_checks import check_estimator @@ -143,4 +143,4 @@ def test_check_estimator(): pass finally: sys.stdout = old_stdout - assert_true(msg in string_buffer.getvalue()) + assert msg in string_buffer.getvalue() diff --git a/imblearn/utils/tests/test_validation.py b/imblearn/utils/tests/test_validation.py index 818464940..62522f532 100644 --- a/imblearn/utils/tests/test_validation.py +++ b/imblearn/utils/tests/test_validation.py @@ -10,8 +10,8 @@ from sklearn.neighbors.base import KNeighborsMixin from sklearn.neighbors import NearestNeighbors -from sklearn.utils.testing import (assert_equal, assert_raises_regex, - assert_warns_message) +from sklearn.utils.testing import assert_raises_regex +from sklearn.utils.testing import assert_warns_message from imblearn.utils import check_neighbors_object from imblearn.utils import check_ratio @@ -22,10 +22,10 @@ def test_check_neighbors_object(): n_neighbors = 1 estimator = check_neighbors_object(name, n_neighbors) assert issubclass(type(estimator), KNeighborsMixin) - assert_equal(estimator.n_neighbors, 1) + assert estimator.n_neighbors == 1 estimator = check_neighbors_object(name, n_neighbors, 1) assert issubclass(type(estimator), KNeighborsMixin) - assert_equal(estimator.n_neighbors, 2) + assert estimator.n_neighbors == 2 estimator = NearestNeighbors(n_neighbors) assert estimator is check_neighbors_object(name, estimator) n_neighbors = 'rnd' @@ -47,16 +47,14 @@ def test_check_ratio_error(): def test_ratio_all_over_sampling(): y = np.array([1] * 50 + [2] * 100 + [3] * 25) - ratio = check_ratio('all', y, 'over-sampling') - assert_equal(ratio, {1: 50, 2: 0, 3: 75}) - ratio = check_ratio('auto', y, 'over-sampling') - assert_equal(ratio, {1: 50, 2: 0, 3: 75}) + for each in ('all', 'auto'): + assert check_ratio(each, y, 'over-sampling') == {1: 50, 2: 0, 3: 75} def test_ratio_all_under_sampling(): y = np.array([1] * 50 + [2] * 100 + [3] * 25) ratio = check_ratio('all', y, 'under-sampling') - assert_equal(ratio, {1: 25, 2: 25, 3: 25}) + assert ratio == {1: 25, 2: 25, 3: 25} def test_ratio_majority_over_sampling(): @@ -68,27 +66,27 @@ def test_ratio_majority_over_sampling(): def test_ratio_majority_under_sampling(): y = np.array([1] * 50 + [2] * 100 + [3] * 25) ratio = check_ratio('majority', y, 'under-sampling') - assert_equal(ratio, {2: 25}) + assert ratio == {2: 25} def test_ratio_not_minority_over_sampling(): y = np.array([1] * 50 + [2] * 100 + [3] * 25) ratio = check_ratio('not minority', y, 'over-sampling') - assert_equal(ratio, {1: 50, 2: 0}) + assert ratio == {1: 50, 2: 0} def test_ratio_not_minority_under_sampling(): y = np.array([1] * 50 + [2] * 100 + [3] * 25) ratio = check_ratio('not minority', y, 'under-sampling') - assert_equal(ratio, {1: 25, 2: 25}) + assert ratio == {1: 25, 2: 25} ratio = check_ratio('auto', y, 'under-sampling') - assert_equal(ratio, {1: 25, 2: 25}) + assert ratio == {1: 25, 2: 25} def test_ratio_minority_over_sampling(): y = np.array([1] * 50 + [2] * 100 + [3] * 25) ratio = check_ratio('minority', y, 'over-sampling') - assert_equal(ratio, {3: 75}) + assert ratio == {3: 75} def test_ratio_minority_under_sampling(): @@ -122,7 +120,7 @@ def test_ratio_dict_over_sampling(): y = np.array([1] * 50 + [2] * 100 + [3] * 25) ratio = {1: 70, 2: 100, 3: 70} ratio_ = check_ratio(ratio, y, 'over-sampling') - assert_equal(ratio_, {1: 20, 2: 0, 3: 45}) + assert ratio_ == {1: 20, 2: 0, 3: 45} ratio = {1: 70, 2: 140, 3: 70} assert_warns_message(UserWarning, "After over-sampling, the number of" " samples (140) in class 2 will be larger than the" @@ -134,7 +132,7 @@ def test_ratio_dict_under_sampling(): y = np.array([1] * 50 + [2] * 100 + [3] * 25) ratio = {1: 30, 2: 45, 3: 25} ratio_ = check_ratio(ratio, y, 'under-sampling') - assert_equal(ratio_, ratio) + assert ratio_ == ratio def test_ratio_float_error(): @@ -151,14 +149,14 @@ def test_ratio_float_over_sampling(): y = np.array([1] * 50 + [2] * 100 + [3] * 25) ratio = 0.5 ratio_ = check_ratio(ratio, y, 'over-sampling') - assert_equal(ratio_, {1: 0, 3: 25}) + assert ratio_ == {1: 0, 3: 25} def test_ratio_float_under_sampling(): y = np.array([1] * 50 + [2] * 100 + [3] * 25) ratio = 0.5 ratio_ = check_ratio(ratio, y, 'under-sampling') - assert_equal(ratio_, {1: 50, 2: 50}) + assert ratio_ == {1: 50, 2: 50} def test_ratio_callable(): @@ -172,7 +170,7 @@ def ratio_func(y): for key in target_stats.keys()} ratio_ = check_ratio(ratio_func, y, 'over-sampling') - assert_equal(ratio_, {1: 50, 2: 0, 3: 75}) + assert ratio_ == {1: 50, 2: 0, 3: 75} def test_ratio_callable_args(): @@ -187,4 +185,4 @@ def ratio_func(y, multiplier): ratio_ = check_ratio(ratio_func, y, 'over-sampling', multiplier=multiplier) - assert_equal(ratio_, {1: 25, 2: 0, 3: 50}) + assert ratio_ == {1: 25, 2: 0, 3: 50}