Skip to content

Commit

Permalink
TST Assert error messages for AdaBoost estimators
Browse files Browse the repository at this point in the history
Follow-up of scikit-learn#21605
  • Loading branch information
jjerphan committed Jan 7, 2022
1 parent 916ad36 commit 25edd20
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion sklearn/ensemble/_weight_boosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ def fit(self, X, y, sample_weight=None):
# Check loss
if self.loss not in ("linear", "square", "exponential"):
raise ValueError(
"loss must be 'linear', 'square', or 'exponential'"
"loss must be 'linear', 'square', or 'exponential'."
f" Got {self.loss!r} instead."
)

Expand Down
13 changes: 9 additions & 4 deletions sklearn/ensemble/tests/test_weight_boosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import pytest
import re

from scipy.sparse import csc_matrix
from scipy.sparse import csr_matrix
Expand Down Expand Up @@ -274,15 +275,19 @@ def test_error():
# Test that it gives proper exception on deficient input.

reg = AdaBoostRegressor(loss="foo")
with pytest.raises(ValueError):
msg = "loss must be 'linear', 'square', or 'exponential'. Got 'foo' instead."
with pytest.raises(ValueError, match=msg):
reg.fit(X, y_class)

clf = AdaBoostClassifier(algorithm="foo")
with pytest.raises(ValueError):
msg = "Algorithm must be 'SAMME' or 'SAMME.R'. Got 'foo' instead."
with pytest.raises(ValueError, match=msg):
clf.fit(X, y_class)

with pytest.raises(ValueError):
AdaBoostClassifier().fit(X, y_class, sample_weight=np.asarray([-1]))
clf = AdaBoostClassifier()
msg = re.escape("sample_weight.shape == (1,), expected (6,)")
with pytest.raises(ValueError, match=msg):
clf.fit(X, y_class, sample_weight=np.asarray([-1]))


def test_base_estimator():
Expand Down

0 comments on commit 25edd20

Please sign in to comment.