Accessing Pipeline Attributes
The source code assumes that pipe and cancer already exist from earlier in the material. This section does not show how they were built, so treat them as given. What matters here is the access path from the pipeline object down to the attribute.
Predicting a Fitted Attribute
Once a pipeline has been fit, you often want to look inside one of its steps. Two common examples are the coefficients of a linear model and the components extracted by principal component analysis (PCA). The pipeline as a whole does not expose these directly, so you need a way to reach the individual step first and then read the attribute you care about. This article begins with a short piece of code, asks you to predict its result, and then explains the mechanism behind that result.
What do you think happens?
The pipeline pipe has been fit and has a step named 'pca'. What shape does pipe.named_steps['pca'].components_ have?
Reveal answer
(2, 30)
The source example reports that, after fitting the pipeline, pipe.named_steps['pca'].components_ yields an array whose shape is (2, 30). The first number is the count of components and the second is the count of features in the input.
If your guess was different, that is useful information. The rest of this article shows exactly which operations produce the (2, 30) array and where each one happens.
Following the Access Path
Read the expression pipe.named_steps['pca'].components_ from left to right. Each part hands a new object to the next part. The pipeline object pipe comes first. Its named_steps attribute gives you the steps, and a lookup with the string 'pca' selects one of them. The final dot reads components_ from that selected step. The diagram below shows each hop in that sequence, along with the kind of object it returns.
Notice that the walk down the hierarchy is not a single operation. It is a dictionary lookup followed by an attribute read, and each of those can be the place where things go wrong.
What named_steps Returns
named_steps is a dictionary from step names to estimators. It is the easiest way to reach the steps of a pipeline.
Because named_steps is a dictionary, the lookup pipe.named_steps['pca'] works the same way any Python dictionary lookup works. The string in brackets is matched against the keys, and the value associated with that key comes back. In this case the value is the estimator in the step named pca. The step name is the key you chose when the pipeline was built, so retrieving a step by name means you do not have to remember its position in the pipeline.
(2, 30)- The first line returns the dictionary of all steps.
- The second line uses the key 'pca' to retrieve one estimator.
- The third line reads the fitted attribute components_ from that estimator.
- The last line checks the shape of the array that was read.
Two Operations in One Expression
The access expression pipe.named_steps['pca'].components_ looks like a single read, but it has two distinct parts. The bracketed lookup returns the step, and the dot followed by an attribute name reads something from that step. Keeping these two parts separate in your head makes problems much easier to diagnose. The lookup and the attribute read happen in one expression, yet they are two separate operations, and if either one fails the error appears at a specific point in that expression. That point is where you should look first.
Read the diagram from left to right and ask at each token what object you now hold. Before the bracket, you hold the dictionary of steps. After the bracket, you hold one estimator. After the final dot, you hold the attribute. An unexpected result always traces back to one of these transitions.
Reading the Fitted Attribute
Attributes such as components_ are produced during fitting. The pipeline as a whole does not expose them, so the attribute only has a meaningful value once the pipeline has been fit. The before-and-after comparison below shows the same expression at two moments.
pipe.named_steps['pca'].components_Pipeline built but not yet fit; the PCA step has no fitted components_ to read.
pipe.named_steps['pca'].components_Pipeline fit; the expression returns an array whose shape is (2, 30).
Reading components_ from a Fitted PCA Step
Given the fitted pipeline pipe from earlier material, retrieve the principal components produced by its PCA step and report their shape.
- 1Lookup the step
pipe.named_steps['pca'] uses the string 'pca' as a dictionary key and returns the PCA estimator stored under that name.
- 2Read the attribute
The dot followed by components_ reads the fitted attribute from that estimator, not from the pipeline.
- 3Check the result
The source reports that this read yields an array whose shape is (2, 30) after the pipeline has been fit.
pipe.named_steps['pca'].components_ is an array of shape (2, 30).
Mistakes That Produce Unexpected Results
When a result differs from what you expected, the source recommends checking three things in order: the fit, the exact step name, and the attribute name. The items below show the most common ways the access path goes wrong.
Common mistake Reading the attribute from the pipeline instead of from the step
What it looks likepipe.components_Why it failsThe pipeline as a whole does not expose these attributes directly, so the read has nothing to find at that level.
Do this insteadReach the step first with pipe.named_steps['pca'], then read components_ from that step.
Common mistake Using a step name that does not match exactly
What it looks likepipe.named_steps['PCA']Why it failsThe bracketed lookup is a dictionary lookup, so the string must match a key exactly, including capitalization.
Do this insteadUse the exact name the step was given, which in this example is 'pca'.
Common mistake Reading a fitted attribute before the pipeline has been fit
What it looks likepipe.named_steps['pca'].components_ on a pipeline that has only been built
Why it failscomponents_ is produced during fitting, so before fitting the step does not hold it.
Do this insteadFit the pipeline first, then read the attribute.
Common mistake Treating the lookup and the attribute read as a single operation, which hides the point where the failure actually happens
Diagnosing a Failed Access
A colleague writes pipe.named_steps['PCA'].components_ and gets an error. Using the order of checks from the source, which check comes first, and which check addresses the capitalization in 'PCA'? Explain which part of the expression the error points to.
Hints
- The source lists three checks in order: the fit, the exact step name, and the attribute name.
- Dictionary keys are matched against the string in brackets, so compare 'PCA' with the name the step was actually given.
- The error appears at the point in the expression where the lookup or the read fails.
Key Takeaways
- The expression pipe.named_steps['pca'].components_ performs two operations: a dictionary lookup that returns a step, then an attribute read from that step.
named_stepsis a dictionary from step names to estimators, so you can retrieve a step by its name rather than by its position.- Fitted attributes such as components_ are produced during fitting, so the pipeline must be fit before they hold a value.
- After fitting, pipe.named_steps['pca'].components_ yields an array whose shape is (2, 30) in the source example.
- When a result is unexpected, check the fit, then the exact step name, then the attribute name, and look at the point in the expression where the failure occurs.