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

Skip to content

Commit f09c7d9

Browse files
FIX Validate estimators in Voting{Classifier,Regressor} (#30649)
Co-authored-by: Omar Salman <[email protected]>
1 parent 5bb2a46 commit f09c7d9

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- :class:`ensemble.VotingClassifier` and :class:`ensemble.VotingRegressor`
2+
validate `estimators` to make sure it is a list of tuples. By `Thomas Fan`_.

sklearn/ensemble/_base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ def __init__(self, estimators):
211211
self.estimators = estimators
212212

213213
def _validate_estimators(self):
214-
if len(self.estimators) == 0:
214+
if len(self.estimators) == 0 or not all(
215+
isinstance(item, (tuple, list)) and isinstance(item[0], str)
216+
for item in self.estimators
217+
):
215218
raise ValueError(
216219
"Invalid 'estimators' attribute, 'estimators' should be a "
217220
"non-empty list of (string, estimator) tuples."

sklearn/ensemble/tests/test_voting.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@
5252
{"estimators": []},
5353
"Invalid 'estimators' attribute, 'estimators' should be a non-empty list",
5454
),
55+
(
56+
{"estimators": [LogisticRegression()]},
57+
"Invalid 'estimators' attribute, 'estimators' should be a non-empty list",
58+
),
59+
(
60+
{"estimators": [(213, LogisticRegression())]},
61+
"Invalid 'estimators' attribute, 'estimators' should be a non-empty list",
62+
),
5563
(
5664
{"estimators": [("lr", LogisticRegression())], "weights": [1, 2]},
5765
"Number of `estimators` and weights must be equal",

0 commit comments

Comments
 (0)