Train-Test Splitting in Supervised Learning
The baseline SVC(C=100) reached 0.63 test accuracy on unscaled features, and scaling the features with MinMaxScaler raised it to 0.97 in the source run.
A Baseline Number to Beat
Every machine learning experiment needs a starting point. Before you can say that a change helped, you need a number that the change is measured against. In supervised learning, that starting point is usually a model trained on the data as it is, with no changes to the input values, and then scored on examples it has never seen. This article uses exactly that kind of experiment. A Support Vector Machine, a supervised learning classifier, is trained on a breast cancer dataset that has been split into a training set and a test set. The model is first fitted on the raw feature values and scored on the raw test set. The score from that first run is the baseline, and every later result in the experiment is only meaningful as a comparison to it.
What do you think happens?
The unscaled SVC scored 0.63 on the test set. After the input features are scaled with MinMaxScaler and the same SVC object is refitted, what do you expect the test accuracy to do?
Reveal answer
It rises substantially, from 0.63 to 0.97 in the source run.
The only thing that changes between the runs is how the input features are prepared. The model, the split, and the test set stay the same, so the jump from 0.63 to 0.97 is attributed to the scaling step in this experiment.
Tracing the Baseline Run
Start by reading the baseline run line by line, because the rest of the exercise depends on it. The dataset is split into training features and test features. The training data is the only data the model learns from. The test data is held back and used only to check how well the learned model works on examples it did not see during fitting. The SVC is fitted on the raw training features and then scored on the raw test features.
The number 0.63 is the fraction of test examples that the unscaled model classified correctly. It is not a measure of the training data, and it is not a measure of how the model would behave on new data beyond this split. It is simply the share of held-out examples the model got right under the raw feature values. Because this number is the reference point, reading it carefully matters more than memorizing it.
Fitting Versus Transforming
The scaled experiment introduces a new object, a scaler, and three calls on it. The fit call is where the scaler learns the parameters it needs from the data it is given. The transform call applies those learned parameters to a dataset. The two calls do different jobs. Fitting looks at data and records something about it. Transforming uses what was recorded to change values. In the source code, fit is called only on the training features, and the test features are passed only to transform.
- The scaler object is created once and is the only object that learns parameters
- fit receives only the training features, so the scaler learns from X_train
- transform is applied to the training features to produce X_train_scaled
- transform is applied to the test features with the same fitted scaler, producing X_test_scaled
- The SVC is trained only on scaled training features and scored only on scaled test features
The key distinction is between fitting the scaler and transforming with it. The scaler is fitted once, from the training features. That single fitted scaler then produces both scaled datasets. The test features are transformed with the parameters learned from training data, not with parameters learned from the test features themselves. If you called fit on the test features, or created a new scaler for the test set, the test set would be scaled according to its own values rather than the training values, and the test score would no longer be produced under the same conditions as the training.
Tracing the Scaled Pipeline
Now trace the scaled experiment in order. The source runs the SVC on scaled features after the same split used in the baseline. The preprocessing is inserted between the split and the model fit, so the model is never shown the raw training values in the scaled run. Each step has a definite input and output, and the final accuracy depends on all of them.
Tracing each call in the scaled run
Given the scaled experiment, determine which dataset each step uses and what the final accuracy measures.
- 1Split
The data is divided into a training set and a test set. Only the training set is available for learning.
- 2Fit the scaler
The scaler is fitted on X_train only. Its learned parameters are determined by the training features.
- 3Transform both sets
The same fitted scaler transforms X_train into X_train_scaled and X_test into X_test_scaled. The test features do not influence the scaler.
- 4Fit the SVC
The SVC is trained on X_train_scaled and its labels. The model never sees X_test during this step.
- 5Score
The fraction of correct predictions is computed on X_test_scaled. This is the held-out accuracy reported for the scaled run.
The scaled test accuracy of 0.97 measures how often the model classified held-out examples correctly after both sets were transformed with a scaler fitted only on training data.
The jump from 0.63 to 0.97 is the size of the change produced by preparing the features. The source does not state a general law that scaling always helps an SVC. It reports the difference in this one experiment, and it is the comparison between the two printed lines that carries the result. What the experiment does show is that the decision boundary the SVC learns depends on the values it is given, so changing how those values are prepared changes the boundary and therefore the predictions on held-out data.
MinMaxScaler and StandardScaler Side by Side
The exercise also runs the same experiment with StandardScaler. The lines use the same fit, transform, and transform pattern, and only the scaler class changes. The two classes do different things to the feature values, so it is worth separating them clearly. MinMaxScaler scales data to a 0 to 1 range. StandardScaler scales data to have zero mean and unit variance. Both are applied in the same place in the workflow: after the split, fitted on training features, and used to transform both sets before the SVC is trained and scored.
| Aspect | MinMaxScaler | StandardScaler |
|---|---|---|
| What it scales to | A 0 to 1 range | Zero mean and unit variance |
| Position in workflow | Fitted on X_train after the split, then used to transform X_train and X_test | Fitted on X_train after the split, then used to transform X_train and X_test |
| Test accuracy in this run | 0.97 | 0.96 |
| Lesson from the run | Scaling raised accuracy from 0.63 | Scaling raised accuracy from 0.63 to nearly the same level |
Both scalers occupy the same workflow slot. They differ in the values they produce.
Where an Unexpected Result Starts
Suppose you run the scaled experiment and the accuracy comes out near the baseline instead of near 0.97. The source's advice is to trace the data through the code in order and ask, at each line, which object and which values are now in play. Most surprising results in this kind of experiment trace back to one of a small number of mistakes in the fit, transform, and score calls.
Common mistake Fitting the scaler on the test features
What it looks likescaler.fit(X_test) followed by transforming X_test with that scaler
Why it failsThe scaler then learns its parameters from the test set. The test set is no longer a clean held-out set, and it is transformed under different parameters than the training set.
Do this insteadCall fit only on X_train, then pass both sets to transform on that same fitted scaler.
Common mistake Creating a new scaler for the test set
What it looks likeA second MinMaxScaler is fitted on X_test and used only to transform X_test
Why it failsThe training features and the test features are now scaled with different learned parameters, so the model sees values on inconsistent scales.
Do this insteadKeep a single scaler object, fitted once on X_train, and reuse it for every transform call.
Common mistake Scoring on the unscaled test features
What it looks likeThe SVC is trained on X_train_scaled but scored on the raw X_test
Why it failsThe model learned on one scale and is evaluated on another. The accuracy then reflects the mismatch, not the model's ability.
Do this insteadScore on X_test_scaled, the output of transform applied to X_test.
Common mistake Assuming the code is correct because each line looks plausible
Why it failsThe source notes that if any arrow connects to the wrong dataset, the final accuracy reflects that error even though every line of code looks reasonable.
Locating the Faulty Step
A student runs the scaled experiment and gets a test accuracy close to 0.63 instead of 0.97. The code creates one MinMaxScaler, calls fit on X_train, transforms X_train into X_train_scaled, and then calls fit on X_test before transforming X_test. The SVC is trained on X_train_scaled and scored on X_test_scaled. Which step is the most likely source of the unexpected result, and what should it be changed to?
Hints
- Trace the scaler object. Ask which call teaches it parameters and which dataset each call receives.
- Compare the fit call on X_test with the rule that fit should see only training features.
- The correction is to keep fit on X_train only and let transform handle X_test.
Without running any code, write the order of the five steps that a correct scaled experiment follows, from the split to the final score. Next to each step, name the dataset it uses. Then explain in two or three sentences why the scaler must be fitted before either transform call.
Hints
- The source describes the sequence as split, fit scaler, transform train, transform test, fit SVC, score.
- The fit call is the step that learns parameters, so transform calls depend on it.
Key Takeaways
- 1The baseline SVC(C=100) reached 0.63 test accuracy on raw features, and the scaled run with MinMaxScaler reached 0.97 in the source experiment.
- 2The scaler is fitted on X_train only, and the same fitted scaler transforms both X_train and X_test before the SVC is trained and scored.
- 3MinMaxScaler scales data to a 0 to 1 range, while StandardScaler scales data to zero mean and unit variance; in this experiment they gave 0.97 and 0.96.
- 4When a scaled experiment gives an unexpected number, trace each fit, transform, and score call to find which object and which dataset each one uses.
At a glance
- The baseline SVC(C=100) reached 0.63 test accuracy on raw features, and scaling with MinMaxScaler raised it to 0.97 in the source run.
- Fit a scaler on X_train only, then use that same fitted scaler to transform both X_train and X_test.
- MinMaxScaler scales to a 0 to 1 range and StandardScaler scales to zero mean and unit variance; both occupy the same slot after the split.
- An unexpected accuracy usually traces to a single fit, transform, or score call that uses the wrong object or the wrong dataset.