Min-max scaling and z-score normalization
2.1.5· AI-ML › Data Preprocessing & Feature Engineering
Overview
Feature scaling numerical features ko ek common scale pe transform karta hai, bina ranges ke differences ko distort kiye. Do fundamental methods hain: min-max scaling (range-based) aur z-score normalization (distribution-based). Dono ek hi problem solve karte hain: bade magnitudes wale features ko distance-based algorithms aur gradient descent mein dominate karne se rokna.

Why Feature Scaling Matters
Jab tumhe ZAROOR scale karna chahiye:
- Gradient descent optimization (neural networks, linear/logistic regression)
- Distance-based algorithms (k-NN, k-means, SVM with RBF kernel)
- PCA aur dimensionality reduction
- Regularization (L1/L2) — unscaled features ko unfairly penalize kiya jaata hai
Jab tumhe scale NAHI karna chahiye:
- Tree-based models (decision trees, random forests, XGBoost) — ye relative thresholds pe split karte hain
- Naive Bayes — ye probabilities ke saath kaam karta hai, distances ke saath nahi
Min-max Scaling (Normalization)
First principles se derivation:
Hum chahte hain ek linear transformation jo map kare:
Pehli condition se:
Doosri condition se:
ke liye solve karte hain:
Back substitute karte hain:
Therefore:
YE FORMULA KYU? Numerator range ko 0 se start karne ke liye shift karta hai. Denominator original range hai, toh isse divide karne par sab kuch [0, 1] mein compress ho jaata hai.
Derivation: Pehle [0, 1] pe map karo, phir se stretch karo aur se shift karo.
Step 1: Range find karo
- ,
- Range
Step 2: Har value pe formula apply karo
- :
- : — Ye step kyun? 25, 20 aur 40 ke beech 1/4 distance pe hai
- : — Kyun? Exactly midpoint hai
- :
- :
Result: [0, 0.25, 0.5, 0.75, 1.0]
Step 1: Generalized formula apply karo , ke saath
Step 2: Calculate karo
- :
- : — Kyun? Middle value, range ke middle pe map hoti hai
- :
Result: [-1.0, 0.0, 1.0]
Properties of Min-max Scaling
Advantages:
- Original distribution shape preserve karta hai (sirf rescale hoti hai)
- Bounded output — un algorithms ke liye useful hai jo specific ranges expect karte hain (jaise neural networks with sigmoid)
- Saare features distance calculations mein equally contribute karte hain
- Interpretable: 0.5 ka matlab hai "min aur max ke beech adha"
Disadvantages:
- Outliers ke liye sensitive — ek single extreme value ya ko drastically change kar deti hai
- Training range ke bahar ka naya test data [0, 1] ke bahar values produce karta hai
- Tab suitable nahi jab new data ka range alag ho sakta hai
Kyun sahi lagta hai: Har dataset [0, 1] pe scale ho jaata hai, consistent lagta hai.
Kyun galat hai: Wahi original value train vs test mein alag scaled values pe map hoti hai. 50 ki value training mein 0.5 ban sakti hai lekin testing mein 0.7, agar test set ki range chhoti hai. Model ne training scale ke basis pe decision boundaries seekhi hain, ab wo misaligned hain.
Fix: Scaler ko sirf training data pe fit karo, wahi transformation test pe apply karo:
scaler.fit(train) # Training se min aur max seekho
train_scaled = scaler.transform(train)
test_scaled = scaler.transform(test) # Training min/max use karoZ-score Normalization (Standardization)
First principles se derivation:
Hum chahte hain transformation aisi ki:
- (mean zero ho)
- (standard deviation 1 ho)
Step 1: Mean condition
Step 2: Variance condition
Hum choose karte hain (order preserve karne ke liye positive).
Step 3: Combine karo
YE FORMULA KYU? distribution ko 0 pe center karta hai. se divide karne par spread exactly 1 standard deviation ho jaata hai.
Ye absolute values ko distribution mein relative positions mein convert karta hai.
Step 1: Statistics calculate karo
- Mean:
- Variance:
- Standard deviation:
Step 2: Formula apply karo
- : — Kyun? 60, average se 1.41 std devs neeche hai
- :
- : — Kyun? Mean pe hai
- :
- :
Result: [-1.41, -0.71, 0, 0.71, 1.41]
Verification: Z-scores ka mean = 0, std dev = 1 ✓
Min-max scaling:
- ,
- Range
- :
- : — Kyun? Saari normal salaries 0 ke paas squish ho gayi hain
- : (outlier)
Notice: Charon normal salaries [0, 0.088] mein aati hain — outlier ki wajah se completely compressed ho gayi hain.
Z-score normalization:
- Mean:
- Variance:
- Standard deviation:
Z-score yahan better kyun hai? Outlier ko ek bada z-score milta hai (≈2.0) lekin normal salaries phir bhi roughly mein spread hoti hain, na ki 0 ke paas ek tiny sliver mein crush hoti hain. Lekin: Z-score bhi outliers se affect hota hai kyunki wo aur dono ko inflate karte hain — extreme cases ke liye, robust scaling (median aur IQR) use karo.
Properties of Z-score Normalization
Advantages:
- Min-max se outliers ke liye kam sensitive (lekin immune nahi)
- Bounded range nahi — kisi bhi value ke saath naya data handle karta hai
- Alag-alag units wale features ke beech comparable scales banata hai
- Normally distributed data ke saath achha kaam karta hai
- Un algorithms ke liye required hai jo standardized inputs assume karte hain (PCA, LDA)
Disadvantages:
- Output bounded nahi hai ([0,1] ki tarah)
- Interpret karna mushkil hai (intuitively z=-0.73 ka kya matlab hai?)
- Roughly bell-shaped distribution assume karta hai
- Extreme outliers ke saath break down karta hai
Kyun sahi lagta hai: Z-score "standardization" hai, universally applicable lagta hai.
Kyun problematic hai: Z-score assume karta hai ki distribution shape roughly preserve hogi. Skew ke saath, zyaadatar z-scores ek taraf cluster karte hain, aur "1 standard deviation" ki interpretation misleading ho jaati hai. Mean outliers se pull hota hai.
Fix: Robust scaling use karo median aur IQR ke saath: Ya pehle log transformation apply karo skew reduce karne ke liye, phir z-score.
When to Use Which Method
| Criterion | Min-max Scaling | Z-score Normalization |
|---|---|---|
| Data distribution | Koi bhi | Normal ya near-normal preferred |
| Outliers present | Avoid karo (sensitive) | Better lekin perfect nahi |
| Bounded output chahiye | Haan [0,1] ya [a,b] | Nahi (unbounded) |
| Neural networks | Good (activations ke liye bounded) | Good (standardized gradients) |
| k-NN, k-means | Good | Good |
| PCA | Acceptable | Preferred (PCA centered data assume karta hai) |
| Test mein naye extremes | Problematic | Better handle karta hai |
| Interpretability | High (range ka percentage) | Medium (mean se std devs) |
Implementation Considerations
Fitting and Transforming
HAMESHA training data pe hi fit karo:
from sklearn.preprocessing import MinMaxScaler, StandardScaler
# Min-max
scaler_minmax = MinMaxScaler()
scaler_minmax.fit(X_train) # Min aur max seekho
X_train_scaled = scaler_minmax.transform(X_train)
X_test_scaled = scaler_minmax.transform(X_test)
# Z-score
scaler_std = StandardScaler()
scaler_std.fit(X_train) # Mean aur std seekho
X_train_scaled = scaler_std.transform(X_train)
X_test_scaled = scaler_std.transform(X_test)Sirf training pe fit kyun karo? Data leakage rokne ke liye. Test set "unseen future data" represent karta hai — uski statistics use karne se test distribution ki information model mein leak ho jaati hai.
Feature-wise vs Global Scaling
Feature-wise (standard): Har column independently scale hoti hai
- Tab use karo jab features ke alag units/meanings hon
- Features ke beech relationships unke apne scale mein preserve karta hai
Global (rare): Saari features ek hi min/max ya mean/std use karte hain
- Sirf tab jab features same quantity represent karte hain (jaise pixel intensities)
Inverse Transformations
Dono methods reversible hain:
Min-max inverse:
Z-score inverse:
Use case: Predictions ko interpretation ke liye original scale pe wapas convert karo.
Recall
Ek 12-saal ke bacche ko samjhao Socho tum aur tumhara dost savings compare kar rahe ho. Tumne 5 hafte mein ₹500 bachaaye. Tumhare dost ne 5 hafte mein $10 bachaaye. Ab tum jaanna chahte ho kaun better saving karta hai, lekin directly compare nahi kar sakte kyunki currencies alag hain!
Min-max scaling aisa hai jaise kehna "Har hafte tumne apne total ka kitna percentage bachaya?" Tum dono apne paise ko 0% se 100% ke percentages mein convert karte ho. Ab compare kar sakte ho!
Z-score aisa hai jaise poochna "Tumhara har hafte tumhare average se kitna upar ya neeche tha?" Agar tumhara average ₹100/hafte hai, aur ek hafte tumne ₹150 bachaye, tum average se +50 upar ho. Agar tumhare dost ka average 3 bachaye, wo apne average se +1 upar hain. Z-score batata hai "tum average se kitne 'typical amounts' upar ya neeche ho" taki alag currencies ke saath bhi compare kar sako.
Dono methods "apples aur oranges" ko same unit mein convert karte hain taki tum unhe fairly compare kar sako!
Connections
- 2.1.01-Data-cleaning-techniques — scaling, missing values aur outliers handle karne ke baad apply hoti hai
- 2.1.04-One-hot-and-label-encoding — categorical encoding, scaling se pehle hoti hai
- 2.2.01-Feature-selection-methods — correlation-based selection se pehle scaling zaroori hai
- 3.1.02-Gradient-descent-optimization — scaling convergence ko dramatically speed up karti hai
- 4.1.05-Principal-component-analysis — PCA ke liye centered/standardized data chahiye
- 5.3.02-Batch-normalization — training ke dauran neural network layer-wise standardization
- 2.1.06-Robust-scaling-and-power-transforms — outliers aur skewed distributions ke liye advanced scaling
#flashcards/ai-ml
Feature scaling kya hai aur ye kyun zaroori hai? :: Feature scaling numerical features ko ek common scale pe transform karta hai. Ye isliye zaroori hai kyunki bade magnitudes wale features distance calculations aur gradient descent mein dominate karte hain, jisse algorithms unhe unfairly overweight karte hain.