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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats_new/upcoming_changes/sklearn.ensemble/30649.fix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- :class:`ensemble.VotingClassifier` and :class:`ensemble.VotingRegressor`
validate `estimators` to make sure it is a list of tuples. By `Thomas Fan`_.
5 changes: 4 additions & 1 deletion sklearn/ensemble/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ def __init__(self, estimators):
self.estimators = estimators

def _validate_estimators(self):
if len(self.estimators) == 0:
if len(self.estimators) == 0 or not all(
isinstance(item, (tuple, list)) and isinstance(item[0], str)
for item in self.estimators
):
raise ValueError(
"Invalid 'estimators' attribute, 'estimators' should be a "
"non-empty list of (string, estimator) tuples."
Expand Down
8 changes: 8 additions & 0 deletions sklearn/ensemble/tests/test_voting.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
{"estimators": []},
"Invalid 'estimators' attribute, 'estimators' should be a non-empty list",
),
(
{"estimators": [LogisticRegression()]},
"Invalid 'estimators' attribute, 'estimators' should be a non-empty list",
),
(
{"estimators": [(213, LogisticRegression())]},
"Invalid 'estimators' attribute, 'estimators' should be a non-empty list",
),
(
{"estimators": [("lr", LogisticRegression())], "weights": [1, 2]},
"Number of `estimators` and weights must be equal",
Expand Down
Loading