Skip to content

Commit

Permalink
Fixed issue 3815. Discrete AdaBoostClassifier now fails early if the …
Browse files Browse the repository at this point in the history
…base classifier if worse than random.
  • Loading branch information
mattilyra committed Nov 7, 2014
1 parent 87dc30b commit 248a503
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion sklearn/ensemble/tests/test_weight_boosting.py
Expand Up @@ -42,7 +42,6 @@
boston.data, boston.target = shuffle(boston.data, boston.target,
random_state=rng)


def test_classification_toy():
"""Check classification on a toy dataset."""
for alg in ['SAMME', 'SAMME.R']:
Expand Down Expand Up @@ -247,6 +246,12 @@ def test_base_estimator():
clf = AdaBoostRegressor(SVR(), random_state=0)
clf.fit(X, y_regr)

# check that an empty discrete ensemble fails early
X_fail = [[1, 1], [1, 1], [1, 1], [1, 1]]
y_fail = ["foo", "bar", 1, 2]
clf = AdaBoostClassifier(SVC(), algorithm="SAMME")
assert_raises(ValueError, clf.fit, X_fail, y_fail)


def test_sample_weight_missing():
from sklearn.linear_model import LinearRegression
Expand Down
4 changes: 4 additions & 0 deletions sklearn/ensemble/weight_boosting.py
Expand Up @@ -546,6 +546,10 @@ def _boost_discrete(self, iboost, X, y, sample_weight):
# Stop if the error is at least as bad as random guessing
if estimator_error >= 1. - (1. / n_classes):
self.estimators_.pop(-1)
if len(self.estimators_) == 0:
raise ValueError('BaseClassifier in AdaBoostClassifier '
'ensemble is worse than random, ensemble '
'can not be fit.')
return None, None, None

# Boost weight using multi-class AdaBoost SAMME alg
Expand Down

0 comments on commit 248a503

Please sign in to comment.