Concepts / Building a Pipeline with Named Steps

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.

  • Programming

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?

  • The whole pipeline

  • The string 'pca'

  • The PCA step itself

Reveal answer
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

Definition

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.

python
Output (expected)
PCA
Notes on the code
  • 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.

scalerstep nameStandardScalerestimatorpcastep namePCAestimator, holdscomponents_ after fitlookup returnslookup returns
How does a string key like 'pca' map to a specific step object inside the pipeline?

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.

Code walkthrough
components = pipe.named_steps['pca'].components_
print(components.shape)
  1. 1
    Step 1: pipe is evaluated first and refers to the pipeline object.
  2. 2
    Step 2: named_steps['pca'] looks up the key 'pca' in the pipeline's dictionary and returns the PCA estimator.
  3. 3
    Step 3: .components_ asks the PCA estimator, not the pipeline, for its learned attribute.
  4. 4
    The 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.
pipePipeline objectnamed_steps['pca']dictionary lookup resultPCA stepestimator that learned theattributecomponents_learned array on the PCAstepdictionary lookupis the estimatorowns attribute
Which object does each part of the chain refer to: the pipeline, the dictionary lookup result, or the PCA step, and where does components_ live?

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.

unfitted PCAno learned attributes yetpipe.fit(X, y)steps learn from data inorderfitted PCAcomponents_ is nowavailableread components_returns the learned arrayfit calledlearnsattribute access
What changes in a PCA step when fit is called, and why does accessing components_ before fitting fail?

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 like

    pipe.named_steps['PCA'] when the step was built as 'pca'

    Why it fails

    Step names are dictionary keys, and keys are matched exactly, including case.

    Do this instead

    Use 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 like
    pipe.components_
    Why it fails

    The components belong to the PCA estimator inside the pipeline, not to the pipeline object itself.

    Do this instead
    Go through the step: pipe.named_steps['pca'].components_.
  • Common mistake

    Reading the attribute before fitting

    What it looks like

    Accessing components_ on a pipeline that has been built but never fitted

    Why it fails

    A learned attribute is produced by fitting, so it does not exist yet.

    Do this instead

    Call fit on the pipeline first, then read the attribute.

  • Common mistake

    Reversing or skipping parts of the chain

    What it looks like

    Writing pipe['pca'].components_ or pipe.named_steps.pca.components_ with the wrong access form

    Why it fails

    Each part of the chain must return the object the next part needs, and the lookup must use the dictionary key form.

    Do this instead

    Write the chain in the order pipe, then named_steps with the name in square brackets, then the attribute.

Access pathWhat it reachesWhat goes wrong
pipe.named_steps['pca'].components_ after fitThe PCA step's learned componentsNothing; this is the correct path
pipe.named_steps['PCA'].components_No step, because the key does not matchThe dictionary lookup fails because the key is case sensitive
pipe.components_The pipeline object, which does not hold this attributeThe attribute is missing on the pipeline, so the lookup fails
pipe.named_steps['pca'].components_ before fitThe PCA step before learningThe 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

EASY

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

  1. 1
    The named_steps attribute is a dictionary from step names to estimators, and it is the easiest way to reach individual steps.
  2. 2
    The chain pipe.named_steps['pca'].components_ reads three operations left to right: the pipeline, the lookup, and the attribute on the step.
  3. 3
    Learned attributes such as components_ belong to the step's estimator and exist only after the pipeline has been fitted.
  4. 4
    Step names must match the names used to build the pipeline exactly, including case.

At a glance

  • named_steps maps 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.

Related concepts

More concepts starting with B