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.
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.
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 value | When it is set | What it controls or holds | Examples named in the source |
|---|---|---|---|
| Constructor parameters | When the model object is constructed | Settings that shape how the model is built | Regularization, complexity control, number of clusters |
| Values learned during fit | When fit runs on the supplied data | What the model comes to know from the data | Not 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.
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.
- 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
Tracing the scaler through its three stages
Follow one scaler from construction to a transformed result, and check what each step returns.
- 1Construct
StandardScaler()creates the object with its settings. No data has been seen yet. - 2Fit
scaler.fit(X_train)learns from the training rows. X is the first argument, and the scaler needs no y. - 3Transform
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.
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
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.
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 failsThe 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 insteadPass the settings to the constructor, then call fit.
Common mistake Passing integer-valued data straight into fit
Why it failsThe source states that the entries of X are assumed to be continuous floating-point numbers.
Do this insteadConvert integer-valued data to floating-point before fitting.
Common mistake Expecting transform to return labels or values to compare with the truth
Why it failsTransform creates a new representation of the input, not a new output such as a classification or regression result.
Do this insteadUse 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 failsThe source states that fit comes first and must precede either of the two ways of applying the model.
Do this insteadAlways 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 failsThe y argument holds target values and is required only for supervised algorithms. Unsupervised methods such as clustering take X alone.
Do this insteadCheck whether the algorithm is supervised before deciding whether to pass y.
Practicing the Fit and Apply Sequence
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.