Skip to content

Commit e4b0849

Browse files
authored
FIX Avoid fitting a pipeline without steps (#31723)
1 parent 9b7a86f commit e4b0849

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

sklearn/pipeline.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ def set_params(self, **kwargs):
320320
return self
321321

322322
def _validate_steps(self):
323+
if not self.steps:
324+
raise ValueError("The pipeline is empty. Please add steps.")
323325
names, estimators = zip(*self.steps)
324326

325327
# validate names
@@ -1289,7 +1291,6 @@ def __sklearn_is_fitted__(self):
12891291
12901292
An empty pipeline is considered fitted.
12911293
"""
1292-
12931294
# First find the last step that is not 'passthrough'
12941295
last_step = None
12951296
for _, estimator in reversed(self.steps):

sklearn/tests/test_pipeline.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,16 @@ def test_pipeline_invalid_parameters():
282282
assert params == params2
283283

284284

285+
def test_empty_pipeline():
286+
X = iris.data
287+
y = iris.target
288+
289+
pipe = Pipeline([])
290+
msg = "The pipeline is empty. Please add steps."
291+
with pytest.raises(ValueError, match=msg):
292+
pipe.fit(X, y)
293+
294+
285295
def test_pipeline_init_tuple():
286296
# Pipeline accepts steps as tuple
287297
X = np.array([[1, 2]])

0 commit comments

Comments
 (0)