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

Skip to content

Commit fb65a0a

Browse files
dokatojnothman
authored andcommitted
[MRG+1] FIX AdaBoost ZeroDivisionError in proba #7501 (#8371)
* FIX AdaBoost ZeroDivisionError in proba #7501 * FIX AdaBoost ZeroDivisionError in proba #7501 - tests corrected * FIX AdaBoost ZeroDivisionError in proba #7501 - tests corrected * FIX #7501 improvements suggested by lesteve introduced * FIX #7501 whats_new file updated * Tweak in rst
1 parent ba8771f commit fb65a0a

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/whats_new.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,14 @@ Enhancements
153153
Bug fixes
154154
.........
155155

156+
- Fixed a bug where :class:`sklearn.ensemble.AdaBoostClassifier` throws
157+
``ZeroDivisionError`` while fitting data with single class labels.
158+
:issue:`7501` by :user:`Dominik Krzeminski <dokato>`.
159+
156160
- Fixed a bug where :func:`sklearn.model_selection.BaseSearchCV.inverse_transform`
157161
returns self.best_estimator_.transform() instead of self.best_estimator_.inverse_transform()
158162
:issue:`8344` by :user:`Akshay Gupta <Akshay0724>`
159163

160-
161164
- Fixed a bug where :class:`sklearn.linear_model.RandomizedLasso` and
162165
:class:`sklearn.linear_model.RandomizedLogisticRegression` breaks for
163166
sparse input.

sklearn/ensemble/tests/test_weight_boosting.py

+9
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ def predict_proba(self, X):
7474
assert_array_equal(np.argmax(samme_proba, axis=1), [0, 1, 1, 1])
7575

7676

77+
def test_oneclass_adaboost_proba():
78+
# Test predict_proba robustness for one class label input.
79+
# In response to issue #7501
80+
# https://github.com/scikit-learn/scikit-learn/issues/7501
81+
y_t = np.ones(len(X))
82+
clf = AdaBoostClassifier().fit(X, y_t)
83+
assert_array_equal(clf.predict_proba(X), np.ones((len(X), 1)))
84+
85+
7786
def test_classification_toy():
7887
# Check classification on a toy dataset.
7988
for alg in ['SAMME', 'SAMME.R']:

sklearn/ensemble/weight_boosting.py

+3
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,9 @@ def predict_proba(self, X):
756756
n_classes = self.n_classes_
757757
X = self._validate_X_predict(X)
758758

759+
if n_classes == 1:
760+
return np.ones((X.shape[0], 1))
761+
759762
if self.algorithm == 'SAMME.R':
760763
# The weights are all 1. for SAMME.R
761764
proba = sum(_samme_proba(estimator, n_classes, X)

0 commit comments

Comments
 (0)