Building a Pipeline with Named Steps
The named_steps attribute of a pipeline is a dictionary from step names to estimators, and it is the easiest way to reach individual steps.
Opening the Pipeline to Look Inside
A pipeline bundles several processing steps into one object, and you often fit the whole pipeline at once. Once it is fitted, you may want to look inside it. For example, you may want the coefficients of a linear model that sits at the end of the pipeline, or the components extracted by a PCA step in the middle. This article shows how to reach into a pipeline and retrieve the attributes of one of its steps.
What do you think happens?
Given a fitted pipeline called pipe with a step named 'pca', what do you think pipe.named_steps['pca'] returns: the whole pipeline, the name 'pca', or the PCA step itself?
Reveal answer
The PCA step itself
The named_steps attribute is a dictionary from step names to estimators. Looking up the key 'pca' returns the estimator stored under that name, which is the PCA step.
What named_steps Holds
The named_steps attribute of a pipeline is a dictionary from step names to estimators. It is the easiest way to reach the individual steps of a pipeline.
Because named_steps is an ordinary dictionary lookup, you pass the step name in square brackets, as in pipe.named_steps['pca']. The result is the estimator object for that step. That means you can read any attribute the estimator has learned, not only the ones the pipeline itself exposes. Step names are dictionary keys, so they must match the names used when the pipeline was built, including case.
PCA- The pipeline is built with two named steps, 'scaler' and 'pca'; these names become the dictionary keys.
- fit is called on the whole pipeline, so each step learns from the data in order.
named_steps['pca'] returns the PCA estimator object, which the variable pca now refers to.
Tracing the Name-to-Step Mapping
Think of named_steps as a small lookup table. Each key is a name you chose when you built the pipeline, and each value is the estimator stored under that name. In a two-step pipeline with a scaler and then PCA, the key 'pca' leads to the PCA estimator and nothing else. Only that estimator carries the components_ attribute.
Reading the Chain One Step at a Time
Reading a learned attribute of one step is a single line that joins three operations: the pipeline, the dictionary lookup, and the attribute access. Python evaluates them from left to right, so each piece takes the result of the piece before it. The first piece is the pipeline object itself. The lookup then returns the PCA step. The final piece asks that step for its components_ attribute.
components = pipe.named_steps['pca'].components_
print(components.shape)- 1Step 1: pipe is evaluated first and refers to the pipeline object.
- 2Step 2:
named_steps['pca'] looks up the key 'pca' in the pipeline's dictionary and returns the PCA estimator. - 3Step 3: .components_ asks the PCA estimator, not the pipeline, for its learned attribute.
- 4The assignment stores that array in components, and print shows its shape.
- pipe is the fitted Pipeline object.
named_steps['pca'] is the dictionary lookup that returns the PCA estimator.- .components_ reads the array that the PCA estimator learned during fit.
- shape is printed to check the size of that learned array.
The chain therefore returns the step's own data. The pipeline object is only the container; it is the PCA estimator inside it that has the components_ attribute. This is why the attribute is read through the step rather than through the pipeline object.
Why Fitting Comes Before Reading
The attribute you read belongs to one of the steps at a particular position in the pipeline. Data passes through the steps in order, so the components from a PCA step come from the data after it has already passed through the steps before it. The attribute is also a learned attribute, which means it is produced when the pipeline is fitted. Read it after fitting the pipeline, and read it through the step.
Mistakes When Accessing Steps and Attributes
Common mistake Using a name that does not match the one used to build the pipeline
What it looks likepipe.named_steps['PCA'] when the step was built as 'pca'
Why it failsStep names are dictionary keys, and keys are matched exactly, including case.
Do this insteadUse the exact name given when the pipeline was built, such as 'pca'.
Common mistake Reading the learned attribute from the pipeline object
What it looks likepipe.components_Why it failsThe components belong to the PCA estimator inside the pipeline, not to the pipeline object itself.
Do this insteadGo through the step: pipe.named_steps['pca'].components_.Common mistake Reading the attribute before fitting
What it looks likeAccessing components_ on a pipeline that has been built but never fitted
Why it failsA learned attribute is produced by fitting, so it does not exist yet.
Do this insteadCall fit on the pipeline first, then read the attribute.
Common mistake Reversing or skipping parts of the chain
What it looks likeWriting pipe['pca'].components_ or pipe.named_steps.pca.components_ with the wrong access form
Why it failsEach part of the chain must return the object the next part needs, and the lookup must use the dictionary key form.
Do this insteadWrite the chain in the order pipe, then named_steps with the name in square brackets, then the attribute.
| Access path | What it reaches | What goes wrong |
|---|---|---|
| pipe.named_steps['pca'].components_ after fit | The PCA step's learned components | Nothing; this is the correct path |
| pipe.named_steps['PCA'].components_ | No step, because the key does not match | The dictionary lookup fails because the key is case sensitive |
| pipe.components_ | The pipeline object, which does not hold this attribute | The attribute is missing on the pipeline, so the lookup fails |
| pipe.named_steps['pca'].components_ before fit | The PCA step before learning | The learned attribute does not exist yet |
Different access paths produce different failures, and only the first path returns the learned components.
Practice Reading a Step
A pipeline named pipe has steps 'scaler' and 'pca', and it has been fitted to data X and y. Write the line that stores the PCA step's components_ in a variable called comps, then state which object the attribute is read from.
Hints
- Start with the pipeline object and add the dictionary lookup with the exact step name.
- The attribute belongs to the step returned by the lookup, not to pipe.
Key Takeaways
- 1The
named_stepsattribute is a dictionary from step names to estimators, and it is the easiest way to reach individual steps. - 2The chain pipe.named_steps['pca'].components_ reads three operations left to right: the pipeline, the lookup, and the attribute on the step.
- 3Learned attributes such as components_ belong to the step's estimator and exist only after the pipeline has been fitted.
- 4Step names must match the names used to build the pipeline exactly, including case.
At a glance
named_stepsmaps each step name to its estimator, so pipe.named_steps['pca'] returns the PCA step.- The attribute chain reads a learned attribute from the step itself, not from the pipeline object.
- Learned attributes like components_ appear only after fitting the pipeline.
- Misspelled step names, attributes read from the pipeline, and reads before fitting are the most common access errors.