Concepts / Estimators and the fit and transform methods

Estimators and the fit and transform methods

The Pipeline class can join any number of estimators, including a regression or clustering estimator as the last step.

  • Programming

Why Pipelines Exist

Many machine learning workflows pass through several stages in a fixed order. Raw data is cleaned and reshaped, then turned into features, and then fed to a model. Keeping track of that order by hand is error-prone. The difficulty grows when the same sequence has to be applied to new data later. A pipeline is the tool that packages such a sequence into a single object, so the stages are defined once and then run together.

This article answers two questions. The first is what a pipeline is allowed to contain. The second is what happens internally when you ask a pipeline to learn from data. The answer to the first question is more permissive than many beginners expect.

What do you think happens?

Many people first meet pipelines as preprocessing followed by a classifier. Could the final step of a pipeline instead be a regression estimator that predicts a continuous value, or a clustering estimator that groups samples?

  • No, only classifiers can end a pipeline

  • Yes, the last step can be a regression or clustering estimator

Reveal answer
Answer:

Yes, the last step can be a regression or clustering estimator.

The Pipeline class is not restricted to preprocessing and classification. It can join any number of estimators, including a regression or clustering estimator as the last step.

Estimators Chained Into One Object

An estimator, in this context, is a component that learns from data. A pipeline chains several estimators in a row, so that the output of one becomes the input of the next. The chain is not fixed to any particular set of tasks. The number of steps is a design choice, and the chain can be any length that makes sense for the problem.

The source gives a concrete arrangement: a pipeline containing feature extraction, feature selection, scaling, and classification, for a total of four steps. Nothing about that particular arrangement is special. It is one valid chain among many.

The same chain could end in a regression estimator that predicts a continuous value, or in a clustering estimator that groups the samples. Changing only the last step does not change how the earlier steps are connected. The first three stages still hand their results forward in the same way.

Raw dataFeature extractionFeature selectionScalingRegression orclustering estimatorinput datatransformed outputtransformed outputtransformed output
How does input data move from the first step through each transformation to the final estimator?

The Transform Requirement for Intermediate Steps

Definition

The only requirement the source states for estimators in a pipeline is this: every step except the last one must have a transform method. The reason is that a non-final step has to produce a new representation of the data, and that new representation is exactly what the following step receives.

AspectNon-final stepLast step
Methods the pipeline calls during fitfit, then transformfit only
Must provide transformYesNot required by this rule
Receives input fromTransform output of the previous stepTransform output of the previous step
Whose output moves onIts transform output moves to the next stepNo following step needs a transformed output

How non-final and last steps differ during Pipeline.fit

Notice the asymmetry. Each non-final step is touched twice: first to learn from its input, and then to produce the output that moves on. The last step is touched once, to learn from the data it receives. This asymmetry is the reason only the non-final steps need a transform method.

Tracing the Calls Inside Pipeline.fit

The requirement above only makes sense once you see the order of calls. When you call Pipeline.fit, the pipeline does not simply fit every step on the original data. It works through the steps in order, and each step receives the output of the step before it.

PipelineFeature extractionFeature selectionScalingClassifier1. fit2. transform3. transformed output4. fit5. transform6. transformed output7. fit8. transform9. transformed output10. fit only
When Pipeline.fit runs, which step's fit and transform run first, which output feeds the next step, and when does the final step's fit run?
Worked example

Following one call sequence through the four-step arrangement

A pipeline has four steps in this order: feature extraction, feature selection, scaling, and classification. Trace what Pipeline.fit does with the training data.

  1. 1
    Feature extraction (not last)

    The pipeline calls fit on feature extraction with the original data, then calls transform on the same step. The transform output is the new representation that moves forward.

  2. 2
    Feature selection (not last)

    The pipeline does not use the original data here. It passes the transform output of feature extraction into feature selection, calls fit on it, and then calls transform to produce its own output.

  3. 3
    Scaling (not last)

    The same pattern repeats. Scaling receives the transform output of feature selection, gets fit, and then gets transform, so its output feeds the classifier.

  4. 4
    Classification (last)

    The classifier receives the transform output of scaling and gets only fit. No transform is called, because no later step needs a transformed result.

Result

Each non-final step is fitted and then transformed in order, and the last step is only fitted on the data it receives. The calls follow the chain, not the original data at every stage.

Why Step Order Changes the Data

Calling rules and data values are separate concerns. The calling rules tell you which methods run and in what order. The data determines what each step learns. Each class in a stand-in example has a fit method that learns something and, where needed, a transform method that returns new data. A helper function follows the rule above: for every step except the last, call fit, then transform, and pass the result onward; for the last step, call fit only.

Trace the data as well as the calls. The first two steps change the values, and the last step learns a summary of the values it is given. If you swapped the order of the first two steps, the intermediate values would change, even though the calling rules would stay exactly the same. This is why the order of steps matters even when the rules are simple.

Mistakes Learners Make With Pipeline Steps

  • Common mistake

    Believing a pipeline can only end in a classifier

    Why it fails

    The Pipeline class can join any number of estimators, and a regression or clustering estimator can be the last step.

    Do this instead

    Treat the final step as the estimator that produces the result, whatever the task is.

  • Common mistake

    Giving a middle step only a fit method

    What it looks like

    A pipeline of feature extraction, a custom step with only fit, and a classifier.

    Why it fails

    Every non-final step must have transform, because its output becomes the input of the next step. Without transform, there is no output to pass forward.

    Do this instead

    Give every non-final step both fit and transform. Only the last step may be a predictor that gets fit alone.

  • Common mistake

    Assuming the last step also needs transform

    Why it fails

    During Pipeline.fit the last step receives only fit, because there is no following step that needs a transformed output.

    Do this instead

    Keep the last step's responsibility to learning from the data it receives.

  • Common mistake

    Thinking step order only affects the calling rules

    Why it fails

    Swapping steps changes the intermediate values each later step receives, even though the rules for which methods run do not change.

    Do this instead

    Check what each step's output looks like before deciding the order.

Checking a Pipeline by Hand

MEDIUM

A pipeline has three steps: step A, step B, and step C. Step A and step B are non-final, and step C is last. Step B provides fit but has no transform method. Using the rule that every non-final step must have transform, explain what would go wrong when Pipeline.fit reaches step B, and why step C cannot simply be fitted on the original data instead.

Hints
  • Ask what step B is supposed to pass to step C after it learns from its input.
  • The rule exists because each non-final step's transform output becomes the input of the next step.
  • Compare what step C should receive with what the pipeline could actually produce.

Key Takeaways

  • A Pipeline can chain any number of estimators, and the last step can be a classifier, a regression estimator, or a clustering estimator.
  • Every step except the last must have a transform method, because its transformed output becomes the input of the next step.
  • During Pipeline.fit, each non-final step receives fit and then transform, using the output of the previous step, while the last step receives only fit.
  • The order of steps changes the values each later step sees, even though the calling rules stay the same.

Related concepts

More concepts starting with E