Feature Scaling (MinMax vs Standard)
Why scale features?
Scaling is mainly important for ML algorithms that use distances or gradients:
- k-NN
- k-means
- SVM
- linear/logistic regression (often)
- neural networks
Tree-based models (like Random Forest) usually don’t need scaling.
Two common scalers
Min-Max scaling
Maps values to a fixed range (usually 0 to 1):
- x_scaled = (x - min) / (max - min)
Standardization (z-score scaling)
Centers and scales to mean=0 and std=1:
- x_scaled = (x - mean) / std
Example (using scikit-learn)
MinMaxScaler vs StandardScaler
import numpy as np
from sklearn.preprocessing import MinMaxScaler, StandardScaler
x = np.array([[10], [20], [30], [100]])
mm = MinMaxScaler()
ss = StandardScaler()
print("MinMax:")
print(mm.fit_transform(x))
print("Standard:")
print(ss.fit_transform(x))MinMaxScaler vs StandardScaler
import numpy as np
from sklearn.preprocessing import MinMaxScaler, StandardScaler
x = np.array([[10], [20], [30], [100]])
mm = MinMaxScaler()
ss = StandardScaler()
print("MinMax:")
print(mm.fit_transform(x))
print("Standard:")
print(ss.fit_transform(x))Important rule: fit on train only
When doing ML:
- Fit scaler on training data
- Transform both train and test using that scaler
This avoids data leakage.
Visualize it
Here’s how features move from raw, mismatched ranges to a common scale a model can use.
flowchart LR A["Raw features \(different ranges\)"] --> B["Fit scaler on training data"] B --> C["Transform features to a common scale"] C --> D["Feed to the model"] B -.->|"fit only on train"| E["Apply same scaler to test"] E --> D
Practical guidance
- If data has strong outliers: StandardScaler can be influenced; consider RobustScaler.
- If you need bounded values (0–1): use MinMaxScaler.
See the shift
Scaling doesn’t change the shape of your data — it changes the ruler. The same four points below get remapped onto a 0–1 range, and again onto a mean-0, std-1 range.
🧪 Try It Yourself
Exercise 1 – Scale to [0, 1] with MinMaxScaler
Exercise 2 – Standardize with StandardScaler
Exercise 3 – Apply the manual z-score formula
Next
With features on the same scale, turn categorical columns into numbers too — continue to One-Hot Encoding.
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
