Concepts / Scaling Training and Test Data the Same Way

Scaling Training and Test Data the Same Way

The same scaling transformation must be applied to the training set and the test set, so that the model sees both sets through one set of rules.

  • Programming

The Tempting Shortcut

Scaling transforms data so that it falls into a common range, often between 0 and 1. This matters because features with very large ranges can dominate a model. When you build a supervised model, you split your data into a training set and a test set, and then you scale both. It is easy to assume that each set can be scaled on its own, using whatever minimum and maximum it happens to contain. That assumption is wrong, and this article explains why.

The source is direct about the requirement: it is important to apply exactly the same transformation to the training set and the test set for the supervised model to work on the test set. The rest of this article shows what goes wrong when that rule is broken, and gives you a way to check your own code.

Where the Scaling Rules Come From

MinMaxScaler works in two stages. The fit step looks at data and records what it needs, which for this scaler is the minimum and the range of each feature. The transform step uses those recorded values to convert data. Fitting is where the scaling parameters are learned, and transforming is where they are applied.

The required workflow is plain: fit the scaler on the training data, and then transform both the training data and the test data with that same scaler. The diagram below traces this single direction of information. The training set is the only source of the learned minimum and range. The test set enters at the transform step, where it is converted using those same values. Nothing flows from the test set back into the scaler.

Training setvalues 0, 5, 10fit()learns minimum and rangeFitted scalerone set of rulestransform()applies the rulesScaled test values0.2, 0.8Test setvalues 2, 8sets rules fromstoressupplies rulesenters hereproduces
Which data sets the scaling rule, and how does that rule reach the test values?

Fit Sets the Rules, Transform Applies Them

The distinction between the two methods is the key to the whole topic. Calling fit on a dataset means that dataset gets to define the rules. Calling transform on a dataset means that dataset is converted using rules that already exist. A correct workflow therefore calls fit exactly once, on the training set, and calls transform on both sets afterward.

The correct mental model is that the test set is a new batch of data that must be read through the rules the model learned from the training set. The test set does not get to define new rules for itself. Once you see that fit is the step that sets the rules and transform is the step that applies them, the error in the misconception becomes visible: calling fit on the test set creates a second, different set of rules.

In the real world

Think of the training set as the data used to calibrate a measuring tool. The test set is a fresh batch of measurements that must be read using that same calibration. If you recalibrate the tool on each new batch, two batches with identical raw readings can end up with different meanings, and the batches can no longer be compared fairly.

A Small Example to Trace by Hand

The example uses numbers small enough to check by hand. The training values are 0, 5 and 10. The test values are 2 and 8. Read the code line by line and follow what each variable holds. The first scaler is the correct one: it is fitted on the training data and then used to transform both sets. The second scaler is the wrong one, included only to show the misconception in action.

python
Output (expected)
[0.  0.5 1. ]
[0.2 0.8]
[0. 1.]
Notes on the code
  • X_train holds the values the model is allowed to learn from
  • scaler.fit(X_train) is the only call that records the minimum and range
  • The test values are passed to transform, not fit
  • bad_scaler.fit(X_test) learns a different minimum and range from the test set

The first two printed lines are the correct result. The training values 0, 5 and 10 become 0.0, 0.5 and 1.0. The test values 2 and 8 become 0.2 and 0.8, which sit inside the range the model learned from the training data. The third line is the misconception in action. The test values 2 and 8 have been stretched across the full range from 0.0 to 1.0, so the same input value now maps to a different output than it did a moment ago.

Predicting the Misconception's Output

Before reading the answer, make your own prediction. The belief to test is that the test set should have its own minimum and maximum, so the scaler should be fitted on the test set and then applied to it.

What do you think happens?

If the test set is scaled with its own minimum and maximum, what do the test values 2 and 8 become?

  • They stay 0.2 and 0.8

  • They become 0.0 and 1.0

  • They stay 2 and 8 unchanged

Reveal answer
Answer:

0.0 and 1.0

The test set has its own minimum of 2 and maximum of 8. Under its own rules, 2 is the bottom of that range and 8 is the top, so they map to 0.0 and 1.0. The same raw test values therefore receive different scaled values than they do under the training rules.

Why Separate Scaling Moves the Points

The same divergence can be seen at the level of a whole dataset. Scaling the two sets together, with the training set fitted and the test set transformed with it, keeps the arrangement of the points. Scaling them separately gives each set a minimum and maximum of 0 and 1, but the test points have shifted relative to the training points. The source describes this as rearranging the data in an arbitrary way, which is not what we want. The relative positions of the points are exactly what consistent scaling protects.

Test value 2scaled to 0.2Test value 2scaled to 0.0Test value 8scaled to 0.8Test value 8scaled to 1.0
How do the scaled test values differ when the test set uses its own minimum and maximum instead of the training set's?

Finding the Line Where Results Diverge

When a scaling pipeline gives unexpected test results, the divergence almost always has a single location: the line where a scaler is fitted. Check each fit call and ask which dataset it received. If a fit call receives anything other than the training set, the transformed test set will be scaled by the wrong rules, even when every other line looks correct. In the example above, the divergence is the line bad_scaler.fit(X_test). Everything before it is correct, and everything after it inherits the mistake.

Split datatraining and test setsWhich set goes intofit?check each fit callfit on training setrules come from trainingdataExpected test output0.2, 0.8fit on test setsecond set of rulesIncorrect test output0.0, 1.0training settest settransform testtransform test
At which fit call do the test values start using different parameters, and which path leads to the incorrect output?
  • Common mistake

    Fitting a second MinMaxScaler on the test set

    What it looks like
    bad_scaler.fit(X_test) followed by bad_scaler.transform(X_test)
    Why it fails

    It creates a second, different set of rules, so the test set is no longer read through the rules the model learned.

    Do this instead

    Call fit only on the training data, then call transform on both the training and test sets with that same scaler.

  • Common mistake

    Scaling each set separately so each has its own minimum and maximum

    Why it fails

    Each set is stretched to the full 0 to 1 range independently, which shifts the test points relative to the training points.

    Do this instead

    Use the training minimum and range for every set, so that the relative positions of the points are preserved.

  • Common mistake

    Assuming a small test set can be scaled on its own

    Why it fails

    A single test value has no spread of its own from which to derive a range, so it cannot be scaled correctly on its own.

    Do this instead

    Let the size of the test set leave the method unchanged. The rules always come from the training data.

Checking Your Own Pipeline

  1. 1
    Find every call to fit in your workflow.
  2. 2
    For each call, identify which dataset it receives.
  3. 3
    Confirm that only the training set reaches fit.
  4. 4
    Confirm that both the training set and the test set reach transform, using the same scaler object.
  5. 5
    If any fit call receives the test set, move the test set to a transform call instead.

Key Takeaways

  • The same scaling transformation must be applied to the training set and the test set, so the model sees both through one set of rules.
  • In MinMaxScaler, fit learns the minimum and range from the training data, and transform applies those values to any dataset, including the test set.
  • Scaling the test set on its own minimum and maximum creates a second set of rules. In the small example, the test values 2 and 8 become 0.0 and 1.0 instead of 0.2 and 0.8.
  • When results are wrong, check each fit call and ask which dataset it received. A fit call on the test set is the line where results diverge.

Related concepts

More concepts starting with S