Right function for normalizing input of sklearn SVM
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
For scikit-learn SVMs, use StandardScaler (z-score normalization) as the default preprocessing choice. It centers features to mean=0 and standard deviation=1, which is what the RBF kernel expects. MinMaxScaler (scaling to [0,1]) is an alternative when features are already bounded or when you want a specific range. Always fit the scaler on training data only and transform both train and test sets using a Pipeline to prevent data leakage.
Why SVMs Need Normalization
SVMs compute distances between data points. Features with larger scales dominate the distance calculation, causing the SVM to ignore smaller-scale features:
StandardScaler (Z-Score Normalization)
Transforms each feature to have mean=0 and standard deviation=1:
Formula: z = (x - mean) / std
Best for: RBF kernel SVM, data with outliers, features with different units.
MinMaxScaler (Min-Max Scaling)
Scales each feature to a fixed range, typically [0, 1]:
Formula: x_scaled = (x - min) / (max - min)
Best for: Features already bounded (pixel values 0-255, probabilities 0-1), linear kernel SVM.
Using Pipeline (Recommended)
Wrap scaling and SVM in a Pipeline to prevent data leakage and simplify code:
Pipeline with GridSearchCV
Other Scalers
| Scaler | Formula | Best For |
StandardScaler | (x - mean) / std | RBF kernel, general use |
MinMaxScaler | (x - min) / (max - min) | Bounded features, linear kernel |
RobustScaler | (x - median) / IQR | Data with outliers |
MaxAbsScaler | x / max(abs(x)) | Sparse data |
Normalizer | x / norm(x) | Text classification, cosine similarity |
Common Pitfalls
- Fitting the scaler on the entire dataset before splitting: Calling
scaler.fit_transform(X)beforetrain_test_splitleaks test set statistics into the training process. Alwaysfiton training data only andtransformthe test set separately. Using aPipelineprevents this automatically. - Choosing
MinMaxScalerfor data with outliers: Outliers compress the majority of values into a narrow range becauseMinMaxScaleruses the actual min and max. UseRobustScalerorStandardScalerwhen outliers are present. - Not scaling test data with the same scaler instance: Calling
scaler.fit_transform(X_test)recomputes the mean and std from the test set, breaking the consistency. Usescaler.transform(X_test)(withoutfit) to apply the training set's parameters. - Using
Normalizerinstead ofStandardScaler:Normalizerscales each row (sample) to unit length, not each column (feature). For SVM feature normalization, you want column-wise scaling (StandardScalerorMinMaxScaler), not row-wise normalization. - Assuming the linear kernel does not need scaling: While the linear kernel is less sensitive to scale than RBF, unscaled features still affect regularization (the
Cparameter). Scale your features regardless of kernel choice for consistent, reproducible results.
Summary
- Use
StandardScaleras the default for SVM preprocessing — it centers features to mean=0, std=1 - Use
MinMaxScalerwhen features are naturally bounded and there are no outliers - Always wrap scaling and SVM in a
Pipelineto prevent data leakage - Fit the scaler on training data only; use
transform(notfit_transform) on test data - The RBF kernel's
gammaparameter is sensitive to feature scale — scaling is essential for good performance
Related reading
- Right order of doing feature selection, PCA and normalization?
- Right parameters for strip_unused_nodes
- \`RNN\` in Tensorflow vs Keras, depreciation of tf.nn.dynamic_rnn
- `RNN` model GRU of word2vec to regression not learning
- Right way to reverse a pandas DataFrame?
- ROC curve for binary classification in python
- Robot exploration algorithm
- roc curve with sklearn python
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.