Skip to content
Open
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
4 changes: 4 additions & 0 deletions sklearn/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ def _iter(self, with_final=True):
if trans is not None and trans != 'passthrough':
yield idx, name, trans

def __len__(self):
"""Return the number of steps in the pipeline."""
return len(self.steps)

def __getitem__(self, ind):
"""Returns a sub-pipeline or a single esimtator in the pipeline

Expand Down
29 changes: 29 additions & 0 deletions sklearn/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,35 @@ def test_pipeline_fit_transform():
assert_array_almost_equal(X_trans, X_trans2)


def test_pipeline_len():
steps = [('transf', Transf()), ('clf', FitParamT())]
pipe = Pipeline(steps)
assert len(pipe) == len(steps)

passthrough_steps = [('transf', Transf()), ('final', 'passthrough')]
pipe_passthrough = Pipeline(passthrough_steps)
assert len(pipe_passthrough) == len(passthrough_steps)

none_steps = [('transf', Transf()), ('final', None)]
pipe_none = Pipeline(none_steps)
assert len(pipe_none) == len(none_steps)


def test_pipeline_slice_with_len():
steps = [
('transf1', Transf()),
('transf2', Transf()),
('clf', FitParamT())
]
pipe = Pipeline(steps)

pipe_all = pipe[:len(pipe)]

assert isinstance(pipe_all, Pipeline)
assert pipe_all.steps == steps
assert len(pipe_all) == len(pipe)


def test_pipeline_slice():
pipe = Pipeline([('transf1', Transf()),
('transf2', Transf()),
Expand Down