From 05f2c9bf7809aca8a7bceffa5f83920f919dfe43 Mon Sep 17 00:00:00 2001 From: hassanalsawadi Date: Mon, 8 Feb 2021 15:47:48 +0300 Subject: [PATCH 1/5] TST replace assert_raise_* by pytest.raises in ensemble module Co-authored-by: feras-oughali --- sklearn/ensemble/tests/test_bagging.py | 78 ++++++++++--------- sklearn/ensemble/tests/test_base.py | 19 ++--- sklearn/ensemble/tests/test_forest.py | 43 +++++----- .../ensemble/tests/test_gradient_boosting.py | 53 +++++++------ sklearn/ensemble/tests/test_iforest.py | 22 +++--- sklearn/ensemble/tests/test_voting.py | 21 +++-- .../ensemble/tests/test_weight_boosting.py | 21 +++-- 7 files changed, 134 insertions(+), 123 deletions(-) diff --git a/sklearn/ensemble/tests/test_bagging.py b/sklearn/ensemble/tests/test_bagging.py index 8b557f50ef5e0..e0fabb08c3a98 100644 --- a/sklearn/ensemble/tests/test_bagging.py +++ b/sklearn/ensemble/tests/test_bagging.py @@ -14,10 +14,8 @@ from sklearn.utils._testing import assert_array_equal from sklearn.utils._testing import assert_array_almost_equal -from sklearn.utils._testing import assert_raises from sklearn.utils._testing import assert_warns from sklearn.utils._testing import assert_warns_message -from sklearn.utils._testing import assert_raise_message from sklearn.dummy import DummyClassifier, DummyRegressor from sklearn.model_selection import GridSearchCV, ParameterGrid @@ -411,28 +409,28 @@ def test_error(): base = DecisionTreeClassifier() # Test max_samples - assert_raises(ValueError, - BaggingClassifier(base, max_samples=-1).fit, X, y) - assert_raises(ValueError, - BaggingClassifier(base, max_samples=0.0).fit, X, y) - assert_raises(ValueError, - BaggingClassifier(base, max_samples=2.0).fit, X, y) - assert_raises(ValueError, - BaggingClassifier(base, max_samples=1000).fit, X, y) - assert_raises(ValueError, - BaggingClassifier(base, max_samples="foobar").fit, X, y) + with pytest.raises(ValueError): + BaggingClassifier(base, max_samples=-1).fit(X, y) + with pytest.raises(ValueError): + BaggingClassifier(base, max_samples=0.0).fit(X, y) + with pytest.raises(ValueError): + BaggingClassifier(base, max_samples=2.0).fit(X, y) + with pytest.raises(ValueError): + BaggingClassifier(base, max_samples=1000).fit(X, y) + with pytest.raises(ValueError): + BaggingClassifier(base, max_samples="foobar").fit(X, y) # Test max_features - assert_raises(ValueError, - BaggingClassifier(base, max_features=-1).fit, X, y) - assert_raises(ValueError, - BaggingClassifier(base, max_features=0.0).fit, X, y) - assert_raises(ValueError, - BaggingClassifier(base, max_features=2.0).fit, X, y) - assert_raises(ValueError, - BaggingClassifier(base, max_features=5).fit, X, y) - assert_raises(ValueError, - BaggingClassifier(base, max_features="foobar").fit, X, y) + with pytest.raises(ValueError): + BaggingClassifier(base, max_features=-1).fit(X, y) + with pytest.raises(ValueError): + BaggingClassifier(base, max_features=0.0).fit(X, y) + with pytest.raises(ValueError): + BaggingClassifier(base, max_features=2.0).fit(X, y) + with pytest.raises(ValueError): + BaggingClassifier(base, max_features=5).fit(X, y) + with pytest.raises(ValueError): + BaggingClassifier(base, max_features="foobar").fit(X, y) # Test support of decision_function assert not hasattr(BaggingClassifier(base).fit(X, y), 'decision_function') @@ -477,11 +475,11 @@ def test_parallel_classification(): assert_array_almost_equal(decisions1, decisions2) X_err = np.hstack((X_test, np.zeros((X_test.shape[0], 1)))) - assert_raise_message(ValueError, "Number of features of the model " - "must match the input. Model n_features is {0} " - "and input n_features is {1} " - "".format(X_test.shape[1], X_err.shape[1]), - ensemble.decision_function, X_err) + with pytest.raises(ValueError, match="Number of features of the model " + "must match the input. Model n_features is {0} " + "and input n_features is {1} " + "".format(X_test.shape[1], X_err.shape[1])): + ensemble.decision_function(X_err) ensemble = BaggingClassifier(SVC(decision_function_shape='ovr'), n_jobs=1, @@ -605,8 +603,9 @@ def test_bagging_sample_weight_unsupported_but_passed(): rng = check_random_state(0) estimator.fit(iris.data, iris.target).predict(iris.data) - assert_raises(ValueError, estimator.fit, iris.data, iris.target, - sample_weight=rng.randint(10, size=(iris.data.shape[0]))) + with pytest.raises(ValueError): + estimator.fit(iris.data, iris.target, + sample_weight=rng.randint(10, size=(iris.data.shape[0]))) def test_warm_start(random_state=42): @@ -639,7 +638,8 @@ def test_warm_start_smaller_n_estimators(): clf = BaggingClassifier(n_estimators=5, warm_start=True) clf.fit(X, y) clf.set_params(n_estimators=4) - assert_raises(ValueError, clf.fit, X, y) + with pytest.raises(ValueError): + clf.fit(X, y) def test_warm_start_equal_n_estimators(): @@ -685,7 +685,8 @@ def test_warm_start_with_oob_score_fails(): # Check using oob_score and warm_start simultaneously fails X, y = make_hastie_10_2(n_samples=20, random_state=1) clf = BaggingClassifier(n_estimators=5, warm_start=True, oob_score=True) - assert_raises(ValueError, clf.fit, X, y) + with pytest.raises(ValueError): + clf.fit(X, y) def test_oob_score_removed_on_warm_start(): @@ -697,7 +698,8 @@ def test_oob_score_removed_on_warm_start(): clf.set_params(warm_start=True, oob_score=False, n_estimators=100) clf.fit(X, y) - assert_raises(AttributeError, getattr, clf, "oob_score_") + with pytest.raises(AttributeError): + getattr(clf, "oob_score_") def test_oob_score_consistency(): @@ -841,9 +843,11 @@ def test_bagging_regressor_with_missing_inputs(): # Verify that exceptions can be raised by wrapper regressor regressor = DecisionTreeRegressor() pipeline = make_pipeline(regressor) - assert_raises(ValueError, pipeline.fit, X, y) + with pytest.raises(ValueError): + pipeline.fit(X, y) bagging_regressor = BaggingRegressor(pipeline) - assert_raises(ValueError, bagging_regressor.fit, X, y) + with pytest.raises(ValueError): + bagging_regressor.fit(X, y) def test_bagging_classifier_with_missing_inputs(): @@ -871,9 +875,11 @@ def test_bagging_classifier_with_missing_inputs(): # Verify that exceptions can be raised by wrapper classifier classifier = DecisionTreeClassifier() pipeline = make_pipeline(classifier) - assert_raises(ValueError, pipeline.fit, X, y) + with pytest.raises(ValueError): + pipeline.fit(X, y) bagging_classifier = BaggingClassifier(pipeline) - assert_raises(ValueError, bagging_classifier.fit, X, y) + with pytest.raises(ValueError): + bagging_classifier.fit(X, y) def test_bagging_small_max_features(): diff --git a/sklearn/ensemble/tests/test_base.py b/sklearn/ensemble/tests/test_base.py index ebeb8f364601f..92adbc94e4192 100644 --- a/sklearn/ensemble/tests/test_base.py +++ b/sklearn/ensemble/tests/test_base.py @@ -6,8 +6,7 @@ # License: BSD 3 clause import numpy as np - -from sklearn.utils._testing import assert_raise_message +import pytest from sklearn.datasets import load_iris from sklearn.ensemble import BaggingClassifier @@ -54,9 +53,9 @@ def test_base_zero_n_estimators(): ensemble = BaggingClassifier(base_estimator=Perceptron(), n_estimators=0) iris = load_iris() - assert_raise_message(ValueError, - "n_estimators must be greater than zero, got 0.", - ensemble.fit, iris.data, iris.target) + with pytest.raises(ValueError, match="n_estimators must be greater\ + than zero, got 0."): + ensemble.fit(iris.data, iris.target) def test_base_not_int_n_estimators(): @@ -65,14 +64,12 @@ def test_base_not_int_n_estimators(): string_ensemble = BaggingClassifier(base_estimator=Perceptron(), n_estimators='3') iris = load_iris() - assert_raise_message(ValueError, - "n_estimators must be an integer", - string_ensemble.fit, iris.data, iris.target) + with pytest.raises(ValueError, match="n_estimators must be an integer"): + string_ensemble.fit(iris.data, iris.target) float_ensemble = BaggingClassifier(base_estimator=Perceptron(), n_estimators=3.0) - assert_raise_message(ValueError, - "n_estimators must be an integer", - float_ensemble.fit, iris.data, iris.target) + with pytest.raises(ValueError, match="n_estimators must be an integer"): + float_ensemble.fit(iris.data, iris.target) def test_set_random_states(): diff --git a/sklearn/ensemble/tests/test_forest.py b/sklearn/ensemble/tests/test_forest.py index 2302ed169bf86..af2d34b8feeb7 100644 --- a/sklearn/ensemble/tests/test_forest.py +++ b/sklearn/ensemble/tests/test_forest.py @@ -29,7 +29,6 @@ from sklearn.utils._testing import assert_almost_equal from sklearn.utils._testing import assert_array_almost_equal from sklearn.utils._testing import assert_array_equal -from sklearn.utils._testing import assert_raises from sklearn.utils._testing import assert_warns from sklearn.utils._testing import assert_warns_message from sklearn.utils._testing import _convert_container @@ -835,12 +834,12 @@ def check_min_samples_split(name): ForestEstimator = FOREST_ESTIMATORS[name] # test boundary value - assert_raises(ValueError, - ForestEstimator(min_samples_split=-1).fit, X, y) - assert_raises(ValueError, - ForestEstimator(min_samples_split=0).fit, X, y) - assert_raises(ValueError, - ForestEstimator(min_samples_split=1.1).fit, X, y) + with pytest.raises(ValueError): + ForestEstimator(min_samples_split=-1).fit(X, y) + with pytest.raises(ValueError): + ForestEstimator(min_samples_split=0).fit(X, y) + with pytest.raises(ValueError): + ForestEstimator(min_samples_split=1.1).fit(X, y) est = ForestEstimator(min_samples_split=10, n_estimators=1, random_state=0) est.fit(X, y) @@ -872,10 +871,10 @@ def check_min_samples_leaf(name): ForestEstimator = FOREST_ESTIMATORS[name] # test boundary value - assert_raises(ValueError, - ForestEstimator(min_samples_leaf=-1).fit, X, y) - assert_raises(ValueError, - ForestEstimator(min_samples_leaf=0).fit, X, y) + with pytest.raises(ValueError): + ForestEstimator(min_samples_leaf=-1).fit(X, y) + with pytest.raises(ValueError): + ForestEstimator(min_samples_leaf=0).fit(X, y) est = ForestEstimator(min_samples_leaf=5, n_estimators=1, random_state=0) est.fit(X, y) @@ -1028,14 +1027,15 @@ def test_memory_layout(name, dtype): @ignore_warnings def check_1d_input(name, X, X_2d, y): ForestEstimator = FOREST_ESTIMATORS[name] - assert_raises(ValueError, ForestEstimator(n_estimators=1, - random_state=0).fit, X, y) + with pytest.raises(ValueError): + ForestEstimator(n_estimators=1, random_state=0).fit(X, y) est = ForestEstimator(random_state=0) est.fit(X_2d, y) if name in FOREST_CLASSIFIERS or name in FOREST_REGRESSORS: - assert_raises(ValueError, est.predict, X) + with pytest.raises(ValueError): + est.predict(X) @pytest.mark.parametrize('name', FOREST_ESTIMATORS) @@ -1122,8 +1122,10 @@ def check_class_weight_errors(name): # Invalid preset string clf = ForestClassifier(class_weight='the larch', random_state=0) - assert_raises(ValueError, clf.fit, X, y) - assert_raises(ValueError, clf.fit, X, _y) + with pytest.raises(ValueError): + clf.fit(X, y) + with pytest.raises(ValueError): + clf.fit(X, _y) # Warning warm_start with preset clf = ForestClassifier(class_weight='balanced', warm_start=True, @@ -1133,11 +1135,13 @@ def check_class_weight_errors(name): # Not a list or preset for multi-output clf = ForestClassifier(class_weight=1, random_state=0) - assert_raises(ValueError, clf.fit, X, _y) + with pytest.raises(ValueError): + clf.fit(X, _y) # Incorrect length list for multi-output clf = ForestClassifier(class_weight=[{-1: 0.5, 1: 1.}], random_state=0) - assert_raises(ValueError, clf.fit, X, _y) + with pytest.raises(ValueError): + clf.fit(X, _y) @pytest.mark.parametrize('name', FOREST_CLASSIFIERS) @@ -1206,7 +1210,8 @@ def check_warm_start_smaller_n_estimators(name): est = ForestEstimator(n_estimators=5, max_depth=1, warm_start=True) est.fit(X, y) est.set_params(n_estimators=4) - assert_raises(ValueError, est.fit, X, y) + with pytest.raises(ValueError): + est.fit(X, y) @pytest.mark.parametrize('name', FOREST_ESTIMATORS) diff --git a/sklearn/ensemble/tests/test_gradient_boosting.py b/sklearn/ensemble/tests/test_gradient_boosting.py index 498e5bf38a675..8135002f6b2d4 100644 --- a/sklearn/ensemble/tests/test_gradient_boosting.py +++ b/sklearn/ensemble/tests/test_gradient_boosting.py @@ -27,8 +27,6 @@ from sklearn.utils._testing import assert_almost_equal from sklearn.utils._testing import assert_array_almost_equal from sklearn.utils._testing import assert_array_equal -from sklearn.utils._testing import assert_raises -from sklearn.utils._testing import assert_raise_message from sklearn.utils._testing import assert_warns from sklearn.utils._testing import assert_warns_message from sklearn.utils._testing import skip_if_32bit @@ -70,7 +68,8 @@ def test_classification_toy(loss): clf = GradientBoostingClassifier(loss=loss, n_estimators=10, random_state=1) - assert_raises(ValueError, clf.predict, T) + with pytest.raises(ValueError): + clf.predict(T) clf.fit(X, y) assert_array_equal(clf.predict(T), true_result) @@ -285,7 +284,8 @@ def test_probability_log(): # Predict probabilities. clf = GradientBoostingClassifier(n_estimators=100, random_state=1) - assert_raises(ValueError, clf.predict_proba, T) + with pytest.raises(ValueError): + clf.predict_proba(T) clf.fit(X, y) assert_array_equal(clf.predict(T), true_result) @@ -319,15 +319,12 @@ def test_check_inputs_predict_stages(): clf = GradientBoostingClassifier(n_estimators=100, random_state=1) clf.fit(x, y) score = np.zeros((y.shape)).reshape(-1, 1) - assert_raise_message(ValueError, - "When X is a sparse matrix, a CSR format is expected", - predict_stages, clf.estimators_, x_sparse_csc, - clf.learning_rate, score) + with pytest.raises(ValueError, match="When X is a sparse matrix,\ + a CSR format is expected"): + predict_stages(clf.estimators_, x_sparse_csc, clf.learning_rate, score) x_fortran = np.asfortranarray(x) - assert_raise_message(ValueError, - "X should be C-ordered np.ndarray", - predict_stages, clf.estimators_, x_fortran, - clf.learning_rate, score) + with pytest.raises(ValueError, match="X should be C-ordered np.ndarray"): + predict_stages(clf.estimators_, x_fortran, clf.learning_rate, score) def test_max_feature_regression(): @@ -416,8 +413,9 @@ def test_staged_predict(): X_test = X[200:] clf = GradientBoostingRegressor() # test raise ValueError if not fitted - assert_raises(ValueError, lambda X: np.fromiter( - clf.staged_predict(X), dtype=np.float64), X_test) + with pytest.raises(ValueError): + (lambda X: np.fromiter(clf.staged_predict(X), + dtype=np.float64))(X_test) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) @@ -437,9 +435,9 @@ def test_staged_predict_proba(): X_train, y_train = X[:200], y[:200] X_test, y_test = X[200:], y[200:] clf = GradientBoostingClassifier(n_estimators=20) - # test raise NotFittedError if not fitted - assert_raises(NotFittedError, lambda X: np.fromiter( - clf.staged_predict_proba(X), dtype=np.float64), X_test) + # test raise NotFittedError if not + with pytest.raises(NotFittedError): + np.fromiter(clf.staged_predict_proba(X_test), dtype=np.float64) clf.fit(X_train, y_train) @@ -501,7 +499,8 @@ def test_degenerate_targets(): clf = GradientBoostingClassifier(n_estimators=100, random_state=1) # classifier should raise exception - assert_raises(ValueError, clf.fit, X, np.ones(len(X))) + with pytest.raises(ValueError): + clf.fit(X, np.ones(len(X))) clf = GradientBoostingRegressor(n_estimators=100, random_state=1) clf.fit(X, np.ones(len(X))) @@ -611,7 +610,8 @@ def test_oob_improvement_raise(): clf = GradientBoostingClassifier(n_estimators=100, random_state=1, subsample=1.0) clf.fit(X, y) - assert_raises(AttributeError, lambda: clf.oob_improvement_) + with pytest.raises(AttributeError): + clf.oob_improvement_ def test_oob_multilcass_iris(): @@ -754,7 +754,8 @@ def test_warm_start_zero_n_estimators(Cls): est = Cls(n_estimators=100, max_depth=1, warm_start=True) est.fit(X, y) est.set_params(n_estimators=0) - assert_raises(ValueError, est.fit, X, y) + with pytest.raises(ValueError): + est.fit(X, y) @pytest.mark.parametrize('Cls', GRADIENT_BOOSTING_ESTIMATORS) @@ -764,7 +765,8 @@ def test_warm_start_smaller_n_estimators(Cls): est = Cls(n_estimators=100, max_depth=1, warm_start=True) est.fit(X, y) est.set_params(n_estimators=99) - assert_raises(ValueError, est.fit, X, y) + with pytest.raises(ValueError): + est.fit(X, y) @pytest.mark.parametrize('Cls', GRADIENT_BOOSTING_ESTIMATORS) @@ -949,7 +951,8 @@ def test_zero_estimator_reg(): est = GradientBoostingRegressor(n_estimators=20, max_depth=1, random_state=1, init='foobar') - assert_raises(ValueError, est.fit, X_reg, y_reg) + with pytest.raises(ValueError): + est.fit(X_reg, y_reg) def test_zero_estimator_clf(): @@ -974,7 +977,8 @@ def test_zero_estimator_clf(): est = GradientBoostingClassifier(n_estimators=20, max_depth=1, random_state=1, init='foobar') - assert_raises(ValueError, est.fit, X, y) + with pytest.raises(ValueError): + est.fit(X, y) @pytest.mark.parametrize('GBEstimator', GRADIENT_BOOSTING_ESTIMATORS) @@ -1034,7 +1038,8 @@ def test_probability_exponential(): clf = GradientBoostingClassifier(loss='exponential', n_estimators=100, random_state=1) - assert_raises(ValueError, clf.predict_proba, T) + with pytest.raises(ValueError): + clf.predict_proba(T) clf.fit(X, y) assert_array_equal(clf.predict(T), true_result) diff --git a/sklearn/ensemble/tests/test_iforest.py b/sklearn/ensemble/tests/test_iforest.py index 46174728c421c..e10777bb86c5e 100644 --- a/sklearn/ensemble/tests/test_iforest.py +++ b/sklearn/ensemble/tests/test_iforest.py @@ -12,7 +12,6 @@ from sklearn.utils._testing import assert_array_equal from sklearn.utils._testing import assert_array_almost_equal -from sklearn.utils._testing import assert_raises from sklearn.utils._testing import assert_warns_message from sklearn.utils._testing import ignore_warnings from sklearn.utils._testing import assert_allclose @@ -92,12 +91,12 @@ def test_iforest_error(): X = iris.data # Test max_samples - assert_raises(ValueError, - IsolationForest(max_samples=-1).fit, X) - assert_raises(ValueError, - IsolationForest(max_samples=0.0).fit, X) - assert_raises(ValueError, - IsolationForest(max_samples=2.0).fit, X) + with pytest.raises(ValueError): + IsolationForest(max_samples=-1).fit(X) + with pytest.raises(ValueError): + IsolationForest(max_samples=0.0).fit(X) + with pytest.raises(ValueError): + IsolationForest(max_samples=2.0).fit(X) # The dataset has less than 256 samples, explicitly setting # max_samples > n_samples should result in a warning. If not set # explicitly there should be no warning @@ -118,11 +117,14 @@ def test_iforest_error(): if issubclass(each.category, UserWarning)] assert len(user_warnings) == 0 - assert_raises(ValueError, IsolationForest(max_samples='foobar').fit, X) - assert_raises(ValueError, IsolationForest(max_samples=1.5).fit, X) + with pytest.raises(ValueError): + IsolationForest(max_samples='foobar').fit(X) + with pytest.raises(ValueError): + IsolationForest(max_samples=1.5).fit(X) # test X_test n_features match X_train one: - assert_raises(ValueError, IsolationForest().fit(X).predict, X[:, 1:]) + with pytest.raises(ValueError): + IsolationForest().fit(X).predict(X[:, 1:]) def test_recalculate_max_depth(): diff --git a/sklearn/ensemble/tests/test_voting.py b/sklearn/ensemble/tests/test_voting.py index 76add81ad1475..d36e71a3c6ff3 100644 --- a/sklearn/ensemble/tests/test_voting.py +++ b/sklearn/ensemble/tests/test_voting.py @@ -7,7 +7,6 @@ from sklearn.utils._testing import assert_almost_equal, assert_array_equal from sklearn.utils._testing import assert_array_almost_equal -from sklearn.utils._testing import assert_raise_message from sklearn.exceptions import NotFittedError from sklearn.linear_model import LinearRegression from sklearn.linear_model import LogisticRegression @@ -70,16 +69,16 @@ def test_notfitted(): ereg = VotingRegressor([('dr', DummyRegressor())]) msg = ("This %s instance is not fitted yet. Call \'fit\'" " with appropriate arguments before using this estimator.") - assert_raise_message(NotFittedError, msg % 'VotingClassifier', - eclf.predict, X) - assert_raise_message(NotFittedError, msg % 'VotingClassifier', - eclf.predict_proba, X) - assert_raise_message(NotFittedError, msg % 'VotingClassifier', - eclf.transform, X) - assert_raise_message(NotFittedError, msg % 'VotingRegressor', - ereg.predict, X_r) - assert_raise_message(NotFittedError, msg % 'VotingRegressor', - ereg.transform, X_r) + with pytest.raises(NotFittedError, match=msg % 'VotingClassifier'): + eclf.predict(X) + with pytest.raises(NotFittedError, match=msg % 'VotingClassifier'): + eclf.predict_proba(X) + with pytest.raises(NotFittedError, match=msg % 'VotingClassifier'): + eclf.transform(X) + with pytest.raises(NotFittedError, match=msg % 'VotingRegressor'): + ereg.predict(X_r) + with pytest.raises(NotFittedError, match=msg % 'VotingRegressor'): + ereg.transform(X_r) def test_majority_label_iris(): diff --git a/sklearn/ensemble/tests/test_weight_boosting.py b/sklearn/ensemble/tests/test_weight_boosting.py index f7c0bad5af193..587e3f538359c 100755 --- a/sklearn/ensemble/tests/test_weight_boosting.py +++ b/sklearn/ensemble/tests/test_weight_boosting.py @@ -11,7 +11,6 @@ from sklearn.utils._testing import assert_array_equal, assert_array_less from sklearn.utils._testing import assert_array_almost_equal -from sklearn.utils._testing import assert_raises, assert_raises_regexp from sklearn.base import BaseEstimator from sklearn.base import clone @@ -270,17 +269,15 @@ def test_importances(): def test_error(): # Test that it gives proper exception on deficient input. - assert_raises(ValueError, - AdaBoostClassifier(learning_rate=-1).fit, - X, y_class) - assert_raises(ValueError, - AdaBoostClassifier(algorithm="foo").fit, - X, y_class) + with pytest.raises(ValueError): + AdaBoostClassifier(learning_rate=-1).fit(X, y_class) - assert_raises(ValueError, - AdaBoostClassifier().fit, - X, y_class, sample_weight=np.asarray([-1])) + with pytest.raises(ValueError): + AdaBoostClassifier(algorithm="foo").fit(X, y_class) + + with pytest.raises(ValueError): + AdaBoostClassifier().fit(X, y_class, sample_weight=np.asarray([-1])) def test_base_estimator(): @@ -307,8 +304,8 @@ def test_base_estimator(): X_fail = [[1, 1], [1, 1], [1, 1], [1, 1]] y_fail = ["foo", "bar", 1, 2] clf = AdaBoostClassifier(SVC(), algorithm="SAMME") - assert_raises_regexp(ValueError, "worse than random", - clf.fit, X_fail, y_fail) + with pytest.raises(ValueError, match="worse than random"): + clf.fit(X_fail, y_fail) def test_sparse_classification(): From cdbdb1b6d97427d1008cf64541a6f7235c674387 Mon Sep 17 00:00:00 2001 From: hassanalsawadi Date: Wed, 10 Feb 2021 14:37:05 +0300 Subject: [PATCH 2/5] TST fix long strings matching Co-authored-by: feras-oughali --- sklearn/ensemble/tests/test_base.py | 4 ++-- sklearn/ensemble/tests/test_gradient_boosting.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sklearn/ensemble/tests/test_base.py b/sklearn/ensemble/tests/test_base.py index 92adbc94e4192..a7909ad442e6b 100644 --- a/sklearn/ensemble/tests/test_base.py +++ b/sklearn/ensemble/tests/test_base.py @@ -53,8 +53,8 @@ def test_base_zero_n_estimators(): ensemble = BaggingClassifier(base_estimator=Perceptron(), n_estimators=0) iris = load_iris() - with pytest.raises(ValueError, match="n_estimators must be greater\ - than zero, got 0."): + with pytest.raises(ValueError, match="n_estimators must be greater" \ + "than zero, got 0."): ensemble.fit(iris.data, iris.target) diff --git a/sklearn/ensemble/tests/test_gradient_boosting.py b/sklearn/ensemble/tests/test_gradient_boosting.py index 8135002f6b2d4..da2c1fb37cc56 100644 --- a/sklearn/ensemble/tests/test_gradient_boosting.py +++ b/sklearn/ensemble/tests/test_gradient_boosting.py @@ -319,8 +319,8 @@ def test_check_inputs_predict_stages(): clf = GradientBoostingClassifier(n_estimators=100, random_state=1) clf.fit(x, y) score = np.zeros((y.shape)).reshape(-1, 1) - with pytest.raises(ValueError, match="When X is a sparse matrix,\ - a CSR format is expected"): + with pytest.raises(ValueError, match="When X is a sparse matrix," \ + "a CSR format is expected"): predict_stages(clf.estimators_, x_sparse_csc, clf.learning_rate, score) x_fortran = np.asfortranarray(x) with pytest.raises(ValueError, match="X should be C-ordered np.ndarray"): From 8547ce128bd4e2ab95d46f4c2822cf2a74c80cc0 Mon Sep 17 00:00:00 2001 From: hassanalsawadi Date: Wed, 10 Feb 2021 14:43:58 +0300 Subject: [PATCH 3/5] TST fix long strings matching using + Co-authored-by: feras-oughali --- sklearn/ensemble/tests/test_base.py | 2 +- sklearn/ensemble/tests/test_gradient_boosting.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn/ensemble/tests/test_base.py b/sklearn/ensemble/tests/test_base.py index a7909ad442e6b..0b050ffb30087 100644 --- a/sklearn/ensemble/tests/test_base.py +++ b/sklearn/ensemble/tests/test_base.py @@ -53,7 +53,7 @@ def test_base_zero_n_estimators(): ensemble = BaggingClassifier(base_estimator=Perceptron(), n_estimators=0) iris = load_iris() - with pytest.raises(ValueError, match="n_estimators must be greater" \ + with pytest.raises(ValueError, match="n_estimators must be greater" + "than zero, got 0."): ensemble.fit(iris.data, iris.target) diff --git a/sklearn/ensemble/tests/test_gradient_boosting.py b/sklearn/ensemble/tests/test_gradient_boosting.py index da2c1fb37cc56..8ed556527c530 100644 --- a/sklearn/ensemble/tests/test_gradient_boosting.py +++ b/sklearn/ensemble/tests/test_gradient_boosting.py @@ -319,7 +319,7 @@ def test_check_inputs_predict_stages(): clf = GradientBoostingClassifier(n_estimators=100, random_state=1) clf.fit(x, y) score = np.zeros((y.shape)).reshape(-1, 1) - with pytest.raises(ValueError, match="When X is a sparse matrix," \ + with pytest.raises(ValueError, match="When X is a sparse matrix," + "a CSR format is expected"): predict_stages(clf.estimators_, x_sparse_csc, clf.learning_rate, score) x_fortran = np.asfortranarray(x) From 8ac23b66bd3342fabf5360f3edc1dd3c1cba190a Mon Sep 17 00:00:00 2001 From: hassanalsawadi Date: Wed, 10 Feb 2021 15:37:52 +0300 Subject: [PATCH 4/5] TST fix strings matching Co-authored-by: feras-oughali --- sklearn/ensemble/tests/test_base.py | 2 +- sklearn/ensemble/tests/test_gradient_boosting.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn/ensemble/tests/test_base.py b/sklearn/ensemble/tests/test_base.py index 0b050ffb30087..3bf112c399470 100644 --- a/sklearn/ensemble/tests/test_base.py +++ b/sklearn/ensemble/tests/test_base.py @@ -53,7 +53,7 @@ def test_base_zero_n_estimators(): ensemble = BaggingClassifier(base_estimator=Perceptron(), n_estimators=0) iris = load_iris() - with pytest.raises(ValueError, match="n_estimators must be greater" + + with pytest.raises(ValueError, match="n_estimators must be greater " + "than zero, got 0."): ensemble.fit(iris.data, iris.target) diff --git a/sklearn/ensemble/tests/test_gradient_boosting.py b/sklearn/ensemble/tests/test_gradient_boosting.py index 8ed556527c530..35140ebf0c1fd 100644 --- a/sklearn/ensemble/tests/test_gradient_boosting.py +++ b/sklearn/ensemble/tests/test_gradient_boosting.py @@ -319,7 +319,7 @@ def test_check_inputs_predict_stages(): clf = GradientBoostingClassifier(n_estimators=100, random_state=1) clf.fit(x, y) score = np.zeros((y.shape)).reshape(-1, 1) - with pytest.raises(ValueError, match="When X is a sparse matrix," + + with pytest.raises(ValueError, match="When X is a sparse matrix, " + "a CSR format is expected"): predict_stages(clf.estimators_, x_sparse_csc, clf.learning_rate, score) x_fortran = np.asfortranarray(x) From f9fba9f227421d0b2db58d6cb8b76fefca4cff5a Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Thu, 11 Feb 2021 11:44:49 +0100 Subject: [PATCH 5/5] nitpicks --- sklearn/ensemble/tests/test_bagging.py | 10 ++++++---- sklearn/ensemble/tests/test_base.py | 4 ++-- sklearn/ensemble/tests/test_gradient_boosting.py | 7 +++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/sklearn/ensemble/tests/test_bagging.py b/sklearn/ensemble/tests/test_bagging.py index e0fabb08c3a98..2dffecca027eb 100644 --- a/sklearn/ensemble/tests/test_bagging.py +++ b/sklearn/ensemble/tests/test_bagging.py @@ -475,10 +475,12 @@ def test_parallel_classification(): assert_array_almost_equal(decisions1, decisions2) X_err = np.hstack((X_test, np.zeros((X_test.shape[0], 1)))) - with pytest.raises(ValueError, match="Number of features of the model " - "must match the input. Model n_features is {0} " - "and input n_features is {1} " - "".format(X_test.shape[1], X_err.shape[1])): + err_msg = ( + f"Number of features of the model must match the input. Model " + f"n_features is {X_test.shape[1]} and input n_features is " + f"{X_err.shape[1]} " + ) + with pytest.raises(ValueError, match=err_msg): ensemble.decision_function(X_err) ensemble = BaggingClassifier(SVC(decision_function_shape='ovr'), diff --git a/sklearn/ensemble/tests/test_base.py b/sklearn/ensemble/tests/test_base.py index 3bf112c399470..3c5b7564380c6 100644 --- a/sklearn/ensemble/tests/test_base.py +++ b/sklearn/ensemble/tests/test_base.py @@ -53,8 +53,8 @@ def test_base_zero_n_estimators(): ensemble = BaggingClassifier(base_estimator=Perceptron(), n_estimators=0) iris = load_iris() - with pytest.raises(ValueError, match="n_estimators must be greater " + - "than zero, got 0."): + err_msg = "n_estimators must be greater than zero, got 0." + with pytest.raises(ValueError, match=err_msg): ensemble.fit(iris.data, iris.target) diff --git a/sklearn/ensemble/tests/test_gradient_boosting.py b/sklearn/ensemble/tests/test_gradient_boosting.py index 35140ebf0c1fd..4de067458c71f 100644 --- a/sklearn/ensemble/tests/test_gradient_boosting.py +++ b/sklearn/ensemble/tests/test_gradient_boosting.py @@ -319,8 +319,8 @@ def test_check_inputs_predict_stages(): clf = GradientBoostingClassifier(n_estimators=100, random_state=1) clf.fit(x, y) score = np.zeros((y.shape)).reshape(-1, 1) - with pytest.raises(ValueError, match="When X is a sparse matrix, " + - "a CSR format is expected"): + err_msg = "When X is a sparse matrix, a CSR format is expected" + with pytest.raises(ValueError, match=err_msg): predict_stages(clf.estimators_, x_sparse_csc, clf.learning_rate, score) x_fortran = np.asfortranarray(x) with pytest.raises(ValueError, match="X should be C-ordered np.ndarray"): @@ -414,8 +414,7 @@ def test_staged_predict(): clf = GradientBoostingRegressor() # test raise ValueError if not fitted with pytest.raises(ValueError): - (lambda X: np.fromiter(clf.staged_predict(X), - dtype=np.float64))(X_test) + np.fromiter(clf.staged_predict(X_test), dtype=np.float64) clf.fit(X_train, y_train) y_pred = clf.predict(X_test)