Skip to content

Commit b4afbee

Browse files
authored
FIX Raise appropriate attribute error in ensemble (#25668)
1 parent 1a669d8 commit b4afbee

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

sklearn/ensemble/_base.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,16 @@ def _validate_estimator(self, default=None):
161161
)
162162

163163
if self.estimator is not None:
164-
self._estimator = self.estimator
164+
self.estimator_ = self.estimator
165165
elif self.base_estimator not in [None, "deprecated"]:
166166
warnings.warn(
167167
"`base_estimator` was renamed to `estimator` in version 1.2 and "
168168
"will be removed in 1.4.",
169169
FutureWarning,
170170
)
171-
self._estimator = self.base_estimator
171+
self.estimator_ = self.base_estimator
172172
else:
173-
self._estimator = default
173+
self.estimator_ = default
174174

175175
# TODO(1.4): remove
176176
# mypy error: Decorated property not supported
@@ -181,13 +181,7 @@ def _validate_estimator(self, default=None):
181181
@property
182182
def base_estimator_(self):
183183
"""Estimator used to grow the ensemble."""
184-
return self._estimator
185-
186-
# TODO(1.4): remove
187-
@property
188-
def estimator_(self):
189-
"""Estimator used to grow the ensemble."""
190-
return self._estimator
184+
return self.estimator_
191185

192186
def _make_estimator(self, append=True, random_state=None):
193187
"""Make and configure a copy of the `estimator_` attribute.

sklearn/ensemble/tests/test_base.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
from sklearn.ensemble import BaggingClassifier
1313
from sklearn.ensemble._base import _set_random_states
1414
from sklearn.linear_model import Perceptron
15+
from sklearn.linear_model import Ridge, LogisticRegression
1516
from collections import OrderedDict
1617
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
1718
from sklearn.pipeline import Pipeline
1819
from sklearn.feature_selection import SelectFromModel
20+
from sklearn import ensemble
1921

2022

2123
def test_base():
@@ -117,3 +119,25 @@ def test_validate_estimator_value_error():
117119
err_msg = "Both `estimator` and `base_estimator` were set. Only set `estimator`."
118120
with pytest.raises(ValueError, match=err_msg):
119121
model.fit(X, y)
122+
123+
124+
# TODO(1.4): remove
125+
@pytest.mark.parametrize(
126+
"model",
127+
[
128+
ensemble.GradientBoostingClassifier(),
129+
ensemble.GradientBoostingRegressor(),
130+
ensemble.HistGradientBoostingClassifier(),
131+
ensemble.HistGradientBoostingRegressor(),
132+
ensemble.VotingClassifier(
133+
[("a", LogisticRegression()), ("b", LogisticRegression())]
134+
),
135+
ensemble.VotingRegressor([("a", Ridge()), ("b", Ridge())]),
136+
],
137+
)
138+
def test_estimator_attribute_error(model):
139+
X = [[1], [2]]
140+
y = [0, 1]
141+
model.fit(X, y)
142+
143+
assert not hasattr(model, "estimator_")

0 commit comments

Comments
 (0)