Concepts / Data Preprocessing Techniques

Data Preprocessing Techniques

Scaling is not cosmetic for every model: in the source experiment, scaling the cancer data raised the SVC's test accuracy from 0.63 to 0.97 with MinMaxScaler.

  • Programming

Predicting What Scaling Will Do

A common assumption among people new to machine learning is that preprocessing is housekeeping. The data is already numbers, the model is the interesting part, and rescaling a few columns seems unlikely to change much. Under this belief, scaling is something you do to tidy up before the real work begins, and you would not expect it to decide whether a model performs well or poorly.

This article tests that belief directly. The same support vector classifier is fitted on the same cancer dataset twice: once on the raw features and once on scaled features. The model settings and the data split stay the same, so the only thing that changes is the scale of the inputs. Keep one question in mind as you read: if the model and the split are held fixed, how much can changing only the scale of the inputs matter?

What do you think happens?

Suppose the SVC is trained with identical settings in both runs, and the only change is that the training and test inputs pass through MinMaxScaler first. Do you expect test accuracy to stay roughly the same, improve slightly, or improve dramatically?

  • Stay roughly the same

  • Improve slightly

  • Improve dramatically

Reveal answer
Answer:

It improved dramatically. In the source experiment, MinMaxScaler raised the SVC's test accuracy from 0.63 to 0.97.

Scaling is not cosmetic for every model. For this SVC on this dataset, the input scale had a very large effect on the score, so preprocessing belongs in the core of the workflow, not in a footnote.

Tracing the Baseline and Scaled Runs

The experiment has two stages, and the first is a baseline. The data is split into training and test portions. An SVC with a large regularization setting is fitted directly on the raw training features, and its accuracy is scored on the raw test features. This number is the reference point that every later result is compared against.

The second stage keeps the same split and the same SVC settings but inserts a scaling step. A MinMaxScaler is created and fitted on the training features only. It is then used to transform both the training features and the test features. The SVC is fitted on the scaled training data and scored on the scaled test data. Each state change in this sequence matters, so track which object holds which data at each point.

Raw training featuresMinMax-scaledfeaturesSVCSVCTest accuracy 0.63Test accuracy 0.97fitscorefitscore
How does scaling the inputs change the SVC's test accuracy when the model settings and the data split stay the same?

Notice that the classifier variable is reused. In the source, the same svm object is fitted a second time, on the scaled training data. The second fit replaces the first model rather than adding to it. When you read the trace, you should therefore compare the two scores as results of two separate fits, not as one model that improved over time.

The Fit and Transform Pattern

Preprocessing is the step that transforms raw data into a format a model can use. Scaling is one kind of preprocessing, and the source treats it as a case where the effect on a supervised model can be large. The sequence of operations matters. The scaler learns its parameters from the training data, and then it applies that same learned transformation to every dataset the model will later be evaluated on.

Every preprocessing class in scikit-learn follows the same two-method pattern. The fit method learns from data, and the transform method applies what was learned. Fit is called once, on training features only. Transform is called on the training features and then on the test features, using the parameters learned during fit.

Training featuresMinMaxScalerTest featuresSVC1. fit on training only2. transform training3. transform test with learned values4. fit on scaled training5. score on scaled test
In what order do the training split, the test split, fit, and transform happen, and why must the test split never influence the fitted scaler?
python
Notes on the code
  • Import the scaler class from scikit-learn instead of rewriting the arithmetic by hand
  • Create the scaler object; it holds no learned values yet
  • Fit learns the minimum and maximum of each feature from the training features only
  • Transform applies those learned values to the training features
  • Transform applies the same learned values to the test features, without refitting

Working Through a Small Scaling Example

Worked example

Scaling a single feature with a learned range

A single feature has training values 10, 20, and 30. A test value of 40 must be transformed with a MinMaxScaler that was fitted on the training values only. What scaled value does the test value receive?

  1. 1
    Fit on training values

    The scaler learns the minimum 10 and the maximum 30 from the training values only. The test value plays no part in this step.

  2. 2
    Recognize the range

    The learned range is 30 minus 10, which equals 20. The scaler maps the minimum to 0 and the maximum to 1 by default.

  3. 3
    Transform the test value

    Using the learned minimum and range, the test value 40 maps to (40 minus 10) divided by 20, which equals 1.5.

  4. 4
    Interpret the result

    The scaled test value is 1.5, which lies outside the 0 to 1 range. This is expected: the learned parameters come from training data, so new data can fall outside them.

Result

The test value 40 becomes 1.5. It is transformed with the training parameters, not with parameters fitted on the test set.

This small example shows why the fit and transform separation exists. If the scaler were refitted on the test values alone, the test value would be the minimum and maximum of its own data and would be mapped to 0 or 1 by construction. The model would then be scored on inputs that were transformed differently from the training inputs it learned from.

Scaling to a Range Versus Standardizing

scikit-learn offers more than one scaler, and they differ in what they aim to produce. Both answer the same practical question, which is how to put features on a comparable scale, but they describe the result in different terms. The diagram below compares them side by side.

Scales to a rangeZero meanTypically 0 to 1Unit variance
How do MinMaxScaler and StandardScaler transform the same feature differently, and what output range or distribution does each produce?

The reason swapping them is easy is that both follow the same fit and transform pattern. Changing the scaler therefore means changing the class name and nothing about the surrounding logic. The StandardScaler version of the experiment follows the same steps as the MinMaxScaler version.

Mistakes That Undo the Benefit of Scaling

  • Common mistake

    Fitting the scaler on the test data

    What it looks like

    Calling fit on the test features, then transforming the test features with that new scaler.

    Why it fails

    The test data then determines the transformation, and the training and test inputs are scaled differently.

    Do this instead

    Fit once on the training features, then transform both the training and test features with that fitted scaler.

  • Common mistake

    Treating preprocessing as housekeeping

    What it looks like

    Skipping scaling because the features are already numbers and the model seems like the important part.

    Why it fails

    In the source experiment, scaling alone moved the SVC's test accuracy from 0.63 to 0.97.

    Do this instead

    Test with and without scaling on your own data before deciding that preprocessing does not matter for a given model.

  • Common mistake

    Rebuilding the scaling arithmetic by hand

    Why it fails

    Small mistakes in hand-written arithmetic are easy to make, and they can silently change the results.

    Do this instead

    Use scikit-learn's preprocessing classes, which already implement the fit and transform logic.

  • Common mistake

    Assuming one scaler always wins

    What it looks like

    Choosing MinMaxScaler for every project because it produced the higher number in one experiment.

    Why it fails

    In the source run, StandardScaler also raised the SVC's test accuracy substantially, though MinMaxScaler produced the higher number. The lesson is that scaling as a category matters for this model.

    Do this instead

    Treat the choice between scalers as something to test on your own data.

Choosing Between Scalers in Practice

The right scaler is an empirical question for the model and dataset at hand. The source does not establish a universal ranking, so the practical habit is to keep the fit and transform structure fixed and swap only the scaler class. Then compare the evaluation scores on held-out test data, which the scaler never saw during fitting.

Why Reduce the Number of Features

Scaling changes the values of features but keeps the number of features the same. A different family of unsupervised transformations changes the representation itself. Dimensionality reduction aims to reduce the number of features in a dataset while preserving important information.

The source names three common motivations: visualization, compressing the data, and finding a representation that is more informative for further processing. These motivations pull in different directions, so it helps to see what each one asks of the reduced representation.

MotivationWhat the reduced representation must doTypical shape or purpose
VisualizationDisplay the data so that a person can see its structureUsually two dimensions, so a scatter plot can show it
CompressionKeep enough information to be useful while using less of itA smaller representation that still carries the important content
Feature extractionProvide a more informative input for a later modelA representation meant to feed further processing

Three motivations for dimensionality reduction and what each asks of the reduced representation

Three Techniques and Their Positions

The source introduces three algorithms that serve these motivations. The first is one of the simplest and most widely used methods, and it applies to all of the motivations above. The other two are more specialized, and each leans toward one particular goal. Comparing how each is positioned is more useful at this stage than memorizing the descriptions.

PositionBreadth of useGoal it leans toward
First algorithmSimplest and most widely used; serves all three motivationsBroad: visualization, compression, and feature extraction
Second algorithmMore specializedOne particular motivation
Third algorithmMore specializedOne particular motivation, different from the second

How the three algorithms in the source are positioned relative to the motivations

Checking Your Understanding

MEDIUM

A colleague fits a MinMaxScaler on the full dataset, including the test rows, and then splits the data for the SVC. Explain what is wrong with this procedure and describe the correct order of fit and transform calls.

Hints
  • Ask which rows the scaler is allowed to learn its parameters from
  • Think about whether the test evaluation is still an honest estimate of performance on new data
  • The correct order is fit on training features only, then transform training and test features
EASY

Suppose you switch from MinMaxScaler to StandardScaler in an existing pipeline. Which lines of code must change, and which stay the same? Explain your reasoning using the fit and transform pattern.

Hints
  • Consider what the shared interface guarantees about the surrounding logic
  • Think about whether the fit call and the transform calls keep the same structure

Key Takeaways

  • Scaling can change model performance a great deal: in the source experiment, MinMaxScaler raised the SVC's test accuracy on the cancer dataset from 0.63 to 0.97 with the same model settings and split.
  • Scalers follow the fit and transform pattern. Fit learns parameters from training data only, and transform applies those learned parameters to both training and test data.
  • MinMaxScaler rescales features into a range such as 0 to 1, while StandardScaler produces zero mean and unit variance. Both are chosen by testing on your own data, not by assumption.
  • Dimensionality reduction reduces the number of features while preserving important information, serving visualization, compression, and feature extraction, which pull in different directions.

Related concepts

More concepts starting with D