Concepts / Preprocessing Data with Transformers

Preprocessing Data with Transformers

The fit method builds the model from data, predict turns a fitted model into a new output, and transform turns a fitted model into a new representation of the input.

  • Programming

A Configured Tool Before Any Data

Picture a learner who has just created a scikit-learn model object and printed it. Nothing in the object has yet learned anything from data. The object is more like a configured tool sitting on a bench: its settings have been chosen, but it has not seen a single example. Three methods decide what happens next. The fit method builds the model from data, predict turns a fitted model into a new output, and transform turns a fitted model into a new representation of the input. This article follows the order in which these methods are used and explains how the data moves through them.

Tracing the Object Through Its Lifecycle

A transformer object passes through a small number of stages. First it is constructed, and at that moment its settings are fixed. Next it is fitted, which means it learns from the data you supply and keeps what it learned. Finally it can be used to transform data. The diagram below shows these stages in order, along with the calls that move the object from one stage to the next.

Constructedsettings chosen, no dataseenFittedmodel built from XAppliedpredict or transform outputfit(X)predict or transform
What state does the object hold before and after fit, and how does that state change when transform is called?

Read the diagram from left to right. Before fit runs, the object holds only the settings you gave it. Its job of learning has not started. Fit is the step that changes the object from a configured tool into a fitted model. Once the object is fitted, it can be applied, either to produce a new output or to produce a new representation of the input. Neither of those application steps replaces fit, which must come first.

Constructor Settings Versus Learned Values

The source guidance is to set any parameters of a model when you construct the model object, not later. These parameters control things such as regularization, complexity control, and the number of clusters the algorithm should find. In other words, these parameters are decisions made before any data is supplied. The fit step then uses those decisions while it builds the model. The table below separates the two kinds of values a model ends up holding.

Kind of valueWhen it is setWhat it controls or holdsExamples named in the source
Constructor parametersWhen the model object is constructedSettings that shape how the model is builtRegularization, complexity control, number of clusters
Values learned during fitWhen fit runs on the supplied dataWhat the model comes to know from the dataNot named individually in the source; fit builds the model from data

Settings chosen at construction versus what fit works out from data

What fit Needs as Input

Every estimator has a fit method, and its job is to build the model. The first argument is always the data X. The call takes a second argument, y, only when the algorithm is supervised. The diagram below annotates a typical call so you can see what each part contributes.

modelthe constructed objectfitbuilds the modelXone row per data pointytargets, supervised only
How are rows and columns of X mapped to samples and features, and where does y fit in the call signature when the algorithm is supervised versus unsupervised?

Fitting and Transforming a Scaler

A common use of transform is preprocessing. The idea is to fit a preprocessing estimator on training data and then transform the data. The example below uses a standard scaler. The scaler is fitted once, and its transform method is then applied to the data it was fitted on. Because the scaler is a preprocessing estimator, it does not need a y argument.

python
Notes on the code
  • X_train holds one row per data point and one column per feature, with floating-point entries
  • The scaler is constructed first, so any settings are fixed before data is supplied
  • fit learns from X_train and stores what it learned in the scaler object
  • transform returns a new representation of X_train rather than a prediction
Worked example

Tracing the scaler through its three stages

Follow one scaler from construction to a transformed result, and check what each step returns.

  1. 1
    Construct

    StandardScaler() creates the object with its settings. No data has been seen yet.

  2. 2
    Fit

    scaler.fit(X_train) learns from the training rows. X is the first argument, and the scaler needs no y.

  3. 3
    Transform

    scaler.transform(X_train) returns a new array. The result has the same rows as the input, each describing the same data point in a new form.

Result

The transformed array has the same number of rows as X_train, and each row is the same data point expressed in the scaled representation.

Choosing Between predict and transform

After fit, there are exactly two ways to apply the model in this framework. The source frames the choice around the kind of result you want. If you want a prediction in the form of a new output, you use predict. If you want a new representation of the input data X, you use transform. The two options start from the same fitted model. What differs is what comes out.

What do you think happens?

After fit, does transform return the same number of rows as the input it was given?

Reveal answer
Answer:

Yes. The transformed result has the same rows as the input, with each row describing the same data point in a new form.

Transform changes the representation of each row, not the number of rows. Predict likewise returns one output per row, but that output is a classification, regression, or clustering result rather than a reworked version of the input.

ClassificationPreprocessingRegressionDimensionalityreductionClusteringFeature extractionFeature selection
Given my goal, does the call return a prediction (labels or values) or a new version of the input data, and what does the output look like in each case?

The left column of the diagram lists the kinds of new output predict creates: a classification, a regression result, or a clustering result. The right column lists the uses of transform: preprocessing, dimensionality reduction, feature extraction, and feature selection. Ask yourself which column matches your goal. If you need an answer about each row, choose predict. If a later step needs the data in a different form, choose transform.

Mistakes When Applying the Three Methods

The following mistakes come up often when learners first apply the three methods. Each one is tied to a rule stated in the source.

  • Common mistake

    Setting model parameters after the object already exists

    Why it fails

    The source says parameters should be set when the model object is constructed. Those settings control regularization, complexity, and the number of clusters, and they are the decisions the fit step uses.

    Do this instead

    Pass the settings to the constructor, then call fit.

  • Common mistake

    Passing integer-valued data straight into fit

    Why it fails

    The source states that the entries of X are assumed to be continuous floating-point numbers.

    Do this instead

    Convert integer-valued data to floating-point before fitting.

  • Common mistake

    Expecting transform to return labels or values to compare with the truth

    Why it fails

    Transform creates a new representation of the input, not a new output such as a classification or regression result.

    Do this instead

    Use predict when you want a new output, and transform when you want a new representation of X.

  • Common mistake

    Calling predict or transform before fit

    Why it fails

    The source states that fit comes first and must precede either of the two ways of applying the model.

    Do this instead

    Always fit the object on data first, then apply predict or transform.

  • Common mistake

    Supplying y to an unsupervised algorithm, or leaving it out of a supervised one

    Why it fails

    The y argument holds target values and is required only for supervised algorithms. Unsupervised methods such as clustering take X alone.

    Do this instead

    Check whether the algorithm is supervised before deciding whether to pass y.

Practicing the Fit and Apply Sequence

EASY

A learner creates a scaler with settings chosen in the constructor, fits it on a training array X_train, and then wants the scaled version of X_train. Write down the three calls in order, and say which method you would use if instead the goal were a predicted class for each row of a supervised model.

Hints
  • Start with the constructor, then the fit call, then the apply call.
  • The scaled version is a new representation of X, so it comes from transform.
  • A predicted class is a new output, so it comes from predict.

Key Takeaways

  • Model parameters such as regularization, complexity control, and the number of clusters are set when the model object is constructed, before any data is supplied.
  • The fit method builds the model from data. Its first argument is X, a NumPy array or SciPy sparse matrix of floating-point values with one row per data point, and it takes an optional y for supervised algorithms only.
  • Fit must come before either way of applying the model. The fitted object is then used with predict or transform.
  • Use predict when the goal is a new output such as a classification, regression, or clustering result. Use transform when the goal is a new representation of the input, such as preprocessing, dimensionality reduction, feature extraction, or feature selection.

Related concepts

More concepts starting with P