Concepts / Feature Scaling with MinMaxScaler and StandardScaler

Feature Scaling with MinMaxScaler and StandardScaler

A second belief says that scaling is so simple that you might as well write the arithmetic yourself and skip the library.

  • Programming

Testing a Belief About Preprocessing

A common first reaction to preprocessing is that it is cosmetic. The data is just being rescaled, the model is still the same model, so the numbers that come out should hardly move. A second belief says that scaling is so simple that you might as well write the arithmetic yourself and skip the library. Both beliefs feel reasonable, and both lead to avoidable mistakes. This article tests them against a concrete example from the cancer dataset used earlier in the chapter.

What do you think happens?

We train a support vector classifier (SVC) on the cancer data twice with identical SVC settings. The first time it gets the raw features. The second time the features are first rescaled to a common range using MinMaxScaler. What happens to the test set accuracy?

Reveal answer
Answer:

The test set accuracy rises from 0.63 to 0.97 when the features are rescaled with MinMaxScaler, even though the SVC itself is unchanged.

The model, its settings, and the data's information content stay the same. Only the numerical scale of the features changes, and that alone moved the test accuracy substantially. Preprocessing is therefore a real decision with measurable consequences, not a cosmetic step.

Tracing the Same Model on Two Feature Scales

Hold the model fixed and change only the input. In the raw version, the SVC is trained on the features exactly as they come from the dataset. In the scaled version, every feature is first passed through MinMaxScaler, which maps each feature into the range from zero to one. The SVC call is written the same way in both cases, so the only new step in the workflow is the scaler. The diagram below traces that single difference from the model's point of view.

SVC on raw featuresTest accuracy 0.63SVC onMinMax-scaledfeaturesTest accuracy 0.97evaluated on test setevaluated on test set
What changes when the features are rescaled before training while the SVC settings stay the same? Only the test accuracy changes in this example, from 0.63 to 0.97.

The source reports this result as a fact of the example, not as a general promise that scaling always lifts accuracy. What the example establishes is narrower and more important: the same model can perform very differently depending on the scale of its inputs, so the scaling choice belongs to the modeling process and must be made deliberately.

How Fit and Transform Divide the Work

Scalers share a fit and transform interface. Fitting means the scaler learns parameters from data. For MinMaxScaler, those parameters are the minimum and maximum of each feature. Transforming means applying the learned parameters to data to produce scaled values. The essential rule is that you fit the scaler on the training data only, and then transform both the training data and the test data with that same fitted scaler. The transform call on the test split reuses the parameters learned from the training split rather than learning new ones from the test data.

Training splitscaler fitlearns min and maxLearned min and maxTransformed trainingsplitTest splitTransformed testsplituses the same learnedvaluesfitstorestransforminputreused, no refit
Which data does the scaler learn its minimum and maximum from, and which data is only transformed using those learned values?
Worked example

Scaling a single feature with a learned minimum and maximum

A feature takes the values 2, 4, and 6 in the training split. The test split contains the value 8. Using MinMaxScaler, which maps values into the range from zero to one, what does each value become?

  1. 1
    Fit on training data

    The scaler learns the minimum 2 and the maximum 6 from the training split only.

  2. 2
    Transform training values

    The value 2 maps to 0, the value 4 maps to 0.5, and the value 6 maps to 1, using the learned range.

  3. 3
    Transform the test value

    The test value 8 is passed through the same fitted scaler, which reuses minimum 2 and maximum 6. It maps to 1.5, which lies outside the zero-to-one range.

  4. 4
    Check the rule

    Had the scaler been refitted on the test split, its learned range would have changed, and the test value would have been scaled against the wrong reference. Reusing the training parameters is what keeps the two splits comparable.

Result

Training values become 0, 0.5, and 1. The test value becomes 1.5 because the learned range from the training split is reused.

MinMaxScaler and StandardScaler Side by Side

The chapter uses two scaling methods. The first is 0-1 scaling, which MinMaxScaler performs, mapping each feature into the range from zero to one. The second is zero mean and unit variance scaling, which StandardScaler performs. The two are different transformations, and they give the same feature values different centers and spreads. The source treats them as interchangeable options for one shared goal: putting features on a comparable footing before modeling.

Range 0 to 1Mean of zeroVariance of one
How do the two transformations map the same feature values differently, and what does each one do to the center and spread of the data?
PropertyMinMaxScalerStandardScaler
Transformation0-1 scalingZero mean and unit variance scaling
Target outcomeEach feature lies in the range from zero to oneEach feature has zero mean and unit variance
Shared interfacefit and transformfit and transform
Choosing between themA real decision with measurable consequencesA real decision with measurable consequences

Both scalers serve the same goal of putting features on a comparable footing, but they reshape the values differently.

Because the interface is shared, you can switch scalers by changing only the class name on the line that creates the scaler. The rest of the workflow, including the fit call on the training split and the transform calls on both splits, stays the same. The chapter demonstrates this by replacing MinMaxScaler with StandardScaler. The important lesson is not which scaler wins in every case, but that choosing and applying a scaler is a real decision.

Why Library Scalers Beat Hand-Written Arithmetic

The second belief from the opening says scaling is simple enough to write yourself. The source addresses this directly. Even though scaling is straightforward arithmetic, it is good practice to use scikit-learn's scaling classes rather than reimplementing them, because hand-written versions are easy to get wrong even in these simple computations.

  • Common mistake

    Assuming preprocessing is cosmetic and cannot change results

    What it looks like

    Training an SVC on raw features and assuming rescaling would barely move the test accuracy.

    Why it fails

    In the source example, rescaling the same features with MinMaxScaler raised the SVC's test accuracy from 0.63 to 0.97 with the model unchanged.

    Do this instead

    Treat scaling as a modeling decision and evaluate its effect on test accuracy.

  • Common mistake

    Refitting the scaler on the test split

    What it looks like

    Calling fit on the test data so that it learns its own minimum and maximum.

    Why it fails

    The test data would then be scaled with parameters different from those used for training, so the two splits are no longer on the same footing.

    Do this instead

    Fit once on the training split, then call transform on both the training and test splits with that same fitted scaler.

  • Common mistake

    Reimplementing the scaling arithmetic by hand

    Why it fails

    Hand-written versions are easy to get wrong, and the library class already provides a tested, consistent implementation of the fit and transform pattern.

    Do this instead

    Use the scikit-learn scaling class and change only the class name when you want a different scaling method.

Tracing a New Test Value Through a Fitted Scaler

Use the worked example's pattern on new numbers. Before you read the hints, try to predict the answer for each step.

EASY

A feature takes the values 10, 20, and 30 in the training split, and the scaler is fitted on that split only. Using a 0-1 scaling rule, what does the training value 20 become, and what does a test value of 40 become when the same fitted scaler transforms it?

Hints
  • Find the learned minimum and maximum from the training split first.
  • The value 20 sits exactly halfway between the minimum and the maximum.
  • Reuse the training minimum and maximum for the test value, and do not recompute them from the test data.

Dimensionality Reduction at a Glance

Scaling changes the values of features. A second family of transformations changes how many features there are. The source introduces these under the heading of dimensionality reduction, feature extraction, and manifold learning. Dimensionality reduction is a technique for reducing the number of features in a dataset while preserving its essential information. The source names three common motivations: visualization, compressing the data, and finding a representation that is more informative for further processing.

  • Principal component analysis: one of the simplest and most widely used methods across all three motivations.
  • Non-negative matrix factorization: commonly used for feature extraction.
  • t-SNE: commonly used for visualization through two-dimensional scatter plots.

This article only introduces these three algorithms. The chapter treats each in more depth, so at this stage the goal is to recognize what dimensionality reduction is for and where each named method fits.

Key Takeaways

  • Preprocessing can change model results: in the source example, MinMaxScaler raised SVC test accuracy from 0.63 to 0.97 with the model unchanged.
  • Scalers share a fit and transform interface. Fit on the training split only, then transform both the training and test splits with that same fitted scaler.
  • MinMaxScaler maps features into the range from zero to one, while StandardScaler gives zero mean and unit variance; both are decisions with measurable consequences.
  • Use scikit-learn's scaling classes rather than hand-written arithmetic, because hand-written versions are easy to get wrong.
  • Dimensionality reduction reduces the number of features while preserving essential information, with principal component analysis, non-negative matrix factorization, and t-SNE as examples.

Related concepts

More concepts starting with F