Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
81 changes: 44 additions & 37 deletions sklearn/ensemble/tests/test_bagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand All @@ -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():
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand Down
19 changes: 8 additions & 11 deletions sklearn/ensemble/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand All @@ -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():
Expand Down
43 changes: 24 additions & 19 deletions sklearn/ensemble/tests/test_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading