diff --git a/sklearn/ensemble/tests/test_bagging.py b/sklearn/ensemble/tests/test_bagging.py index a08d421a75dfe..e7cb11185fa5c 100644 --- a/sklearn/ensemble/tests/test_bagging.py +++ b/sklearn/ensemble/tests/test_bagging.py @@ -14,9 +14,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_raise_message - from sklearn.dummy import DummyClassifier, DummyRegressor from sklearn.model_selection import GridSearchCV, ParameterGrid from sklearn.ensemble import BaggingClassifier, BaggingRegressor @@ -418,28 +415,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') @@ -484,11 +481,13 @@ 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) + 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'), n_jobs=1, @@ -612,8 +611,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): @@ -646,7 +646,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(): @@ -692,7 +693,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(): @@ -704,7 +706,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(): @@ -848,9 +851,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(): @@ -878,9 +883,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..3c5b7564380c6 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) + err_msg = "n_estimators must be greater than zero, got 0." + with pytest.raises(ValueError, match=err_msg): + 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 f97c956327fd5..89ded326d21aa 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 _convert_container from sklearn.utils._testing import ignore_warnings from sklearn.utils._testing import skip_if_no_parallel @@ -833,12 +832,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) @@ -870,10 +869,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) @@ -1026,14 +1025,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) @@ -1120,8 +1120,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, @@ -1137,11 +1139,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) @@ -1210,7 +1214,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 1fbec105e3cb4..57ac93f52d0d3 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 skip_if_32bit from sklearn.exceptions import DataConversionWarning from sklearn.exceptions import NotFittedError @@ -68,7 +66,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) @@ -283,7 +282,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) @@ -317,15 +317,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) + 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) - 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(): @@ -414,8 +411,8 @@ 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): + np.fromiter(clf.staged_predict(X_test), dtype=np.float64) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) @@ -435,9 +432,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) @@ -499,7 +496,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))) @@ -615,7 +613,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(): @@ -758,7 +757,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) @@ -768,7 +768,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) @@ -953,7 +954,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(): @@ -978,7 +980,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) @@ -1037,7 +1040,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 2d7e059baaa03..de0c56fff793b 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 ignore_warnings from sklearn.utils._testing import assert_allclose @@ -91,12 +90,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 @@ -117,11 +116,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():