Applying Kernel SVMs to Scaled Data
A common first reaction to MinMaxScaler is to assume that scaling places every feature into the same tidy box.
The Expectation That Breaks
A common first reaction to MinMaxScaler is to assume that scaling places every feature into the same tidy box. You fit the scaler, you transform the data, and you expect every number in the result to sit between 0 and 1. For the training data, that expectation is correct. For the test data, it is often wrong. The mismatch is a useful place to learn how scaling really works.
This article follows one workflow from start to finish: load a dataset, split it, scale it, and then check the results. Along the way you will see where the correct behavior comes from, why the test set can land outside the 0 to 1 range, and how to track down the step responsible when a result looks different from what you expected.
What do you think happens?
You have fitted a MinMaxScaler on a training set and then used it to transform a separate test set. What do you expect the minimum and maximum of each test feature to be after scaling?
Reveal answer
Not necessarily 0 and 1. The training set is guaranteed to land in 0 to 1, but the test set is transformed with the training minimum and training range, so its values can fall below 0 or above 1.
The scaler learned its minimum and range from the training data only. The same two numbers are reused for the test data, so the test data has no reason to fit the box exactly.
Where Preprocessing Fits in the Workflow
The workflow in this lesson trains a kernel SVM, which is a supervised model, on the cancer dataset from an earlier chapter. Preprocessing methods such as scalers are usually applied before a supervised algorithm is applied. The model, however, still has to be evaluated on data it has not seen. For that reason the dataset is split into a training set and a test set before any scaling happens.
Preprocessing is the step that transforms the raw features before a model sees them. In this workflow, the split into training and test sets is done first, so that the test set remains a stand-in for data the model has never encountered.
The cancer dataset has 569 data points, and each one is described by 30 measurements. The split sends 426 samples to the training set and 143 samples to the test set. Only the number of rows is divided; the feature count stays at 30 in both sets.
(426, 30)
(143, 30)- The split happens before any scaler is created or fitted.
- Both sets keep 30 feature columns; only the rows are divided.
How the Scaler Learns and Applies
Scaling follows a fixed sequence. The class that implements the preprocessing is imported and instantiated, just as the supervised models from earlier were created. The instance is then fitted, transformed on the training data, and transformed on the test data. Each step has a specific job, and the order matters.
Tracing One Feature Through Scaling
The cancer dataset is too large to inspect by hand, so this example uses one feature with a handful of values. The training values are 2, 4, and 6. That means the training minimum is 2 and the training range is 4. The test values are 1 and 8. The arithmetic is simple enough to check on paper before running any code.
Scaling a Single Feature with a Fitted Scaler
A scaler is fitted on training values 2, 4, and 6, then used to transform test values 1 and 8. What are the scaled values?
- 1Fit on training values
The training minimum is 2 and the training maximum is 6, so the training range is 6 minus 2, which equals 4.
- 2Transform the training values
The value 2 becomes $\frac{2-2}{4} = 0$, the value 4 becomes $\frac{4-2}{4} = 0.5$, and the value 6 becomes $\frac{6-2}{4} = 1$. The training set lands exactly in 0 to 1.
- 3Transform the test value 1
The test value 1 becomes $\frac{1-2}{4} = -0.25$. It is below the training minimum, so the scaled result is below 0.
- 4Transform the test value 8
The test value 8 becomes $\frac{8-2}{4} = 1.5$. It is above the training maximum, so the scaled result is above 1.
The training values scale to 0, 0.5, and 1. The test values scale to -0.25 and 1.5. The test set is not confined to 0 to 1, and nothing went wrong.
Why the Test Set Can Fall Outside 0 to 1
The source explains this directly. MinMaxScaler, like the other scalers, applies exactly the same transformation to the training and test sets. The transform subtracts the training minimum and divides by the training range. The test set has its own minimum and range, which might be different, so its scaled values can land outside the training box. Some of the scaled cancer test features do exactly this.
Locating Where the Result Diverges
When a scaled result looks wrong, the useful question is which step first produced a value you did not expect. In this workflow, the training set is the reference. If the training features show a minimum of 0 and a maximum of 1 but the test features do not, the divergence has not come from fit or from transform on training data. It has come from transform on the test set, and the cause is that the test values lie beyond the training minimum or maximum. Checking the training minimum and range against the test values locates the point of divergence quickly.
Common mistake Expecting the test set to land exactly in 0 to 1
What it looks likeTest values 1 and 8 with training values 2, 4, and 6 produce -0.25 and 1.5.
Why it failsThe scaler reuses the training minimum and range, and the test set's own values are not guaranteed to sit inside that range.
Do this insteadExpect the training set to fall in 0 to 1 and treat test values outside that interval as normal.
Common mistake Refitting the scaler on the test set
What it looks likeCalling scaler.fit(X_test) before transforming the test data.
Why it failsThis replaces the training minimum and range with test-based numbers, so the test set is scaled differently from the training set and the model sees inconsistent features.
Do this insteadCall fit only on X_train, and call transform on X_test.
Common mistake Scaling before splitting
What it looks likeFitting the scaler on all 569 rows and then splitting the scaled data.
Why it failsThe scaler has then seen the test data's minimum and maximum, so the test set is no longer fully unseen by the preprocessing step.
Do this insteadSplit first, then fit the scaler on the training features only.
Common mistake Passing labels to the scaler
| Question | Split first, then scale | Scale first, then split |
|---|---|---|
| Which data does the scaler see? | Only the training features | All rows, including the test features |
| Where do the minimum and range come from? | The training set alone | The whole dataset, including test values |
| What does the model learn about the test data? | Nothing beforehand; the test set stays unseen | Test-set extremes influence the preprocessing |
| Can the test set land outside 0 to 1? | Yes, when test values exceed the training range | The test set is compressed toward the full-data range, so the evaluation is less honest |
Two orderings of the split and the scaler, and what each one exposes to the scaler.
Practice Tracing a New Feature
A MinMaxScaler is fitted on one feature whose training values are 10, 20, and 50. It is then used to transform test values 5 and 60. Compute the scaled value of each test value, and state whether each result lies inside the range 0 to 1.
Hints
- The training minimum is 10 and the training maximum is 50, so the training range is 40.
- Use $\frac{x-10}{40}$ for each test value x.
- A negative result or a result above 1 means the test value lies outside the training range.
Summary of the Workflow
The workflow splits the data first, fits the scaler on the training features only, transforms both sets with the same training minimum and range, and only then trains the supervised model. Each step has a single job, and the order is what keeps the test set honest. When a test result falls outside 0 to 1, the scaler is behaving as designed, and the place to look for an unexpected result is the transform applied to the test set.
At a glance
- Split the data into training and test sets before any scaling, because the test set is needed to evaluate the model on data it has not seen.
- MinMaxScaler's fit learns each feature's minimum and range from the training features only; labels are not passed to it.
- transform applies the same training-based minimum and range to both sets, which guarantees the training set lands in 0 to 1 but not the test set.
- Test values below 0 or above 1 after scaling are expected when they lie outside the training range, and the divergence is found by checking the transform on the test set.