← Lesson
BitWithBite
AI & Machine Learning · Quick Reference

1.2 Preprocessing: Scaling, Normalization & Encoding Cheat Sheet

AI & Machine Learning
In one line: Imagine you're predicting house prices. Two features:

Key Ideas

1Why Preprocessing Matters. Imagine you're predicting house prices. Two features:
2StandardScaler — Zero Mean, Unit Variance. The most common scaler. For each feature, it subtracts the mean and divides by the standard deviation:
3MinMaxScaler — Compress to a Fixed Range. Scales each feature to a specific range, usually [0, 1]:
4RobustScaler — Handles Outliers. Uses the median and the interquartile range (IQR) instead of mean and std. Outliers have much less influence:
5Which Scaler to Choose Decision guide Does your data have significant outliers? ├── Yes → RobustScaler └── No → Does the model need values in [0,1]? ├── Yes → MinMaxScaler └── No → StandardScaler (default) ScalerCenters onScale unitOutlier resistantBest for StandardScalerMeanStd deviationNoMost models (default) MinMaxScalerMin valueValue rangeNoNeural nets, image data RobustScalerMedianIQRYesData with real outliers None———Tree-based models (RF, XGBoost) Encoding Categorical Variables. Models work with numbers. A column containing "red", "green", "blue" needs to become numbers. There are two main approaches, and choosing wrong causes subtle bugs.

Code Examples

from sklearn.preprocessing import StandardScaler import numpy as np X_train = np.array([[25], [50], [75], [150]]) X_test = np.array([[40], [100]]) scaler = StandardScaler() scaler.fit(X_train) # learn μ and σ from train X_train_sc ...
from sklearn.preprocessing import OrdinalEncoder import numpy as np # ✅ CORRECT: size has a real order X = np.array([['small'], ['large'], ['medium'], ['small']]) enc = OrdinalEncoder(categories=[['small', 'medium', 'large']]) enc.fit(X) print(enc...
from sklearn.preprocessing import OneHotEncoder import numpy as np X = np.array([['red'], ['green'], ['blue'], ['red']]) enc = OneHotEncoder(sparse_output=False) enc.fit(X) X_encoded = enc.transform(X) print(enc.categories_) # [['blue', 'gre...