python 22 lines · 1 tab

Scaling and normalization choices for different model families

1 tab
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler
from sklearn.linear_model import LogisticRegression

standard_pipeline = Pipeline([
    ('scaler', StandardScaler()),
    ('model', LogisticRegression(max_iter=1000)),
])

robust_pipeline = Pipeline([
    ('scaler', RobustScaler()),
    ('model', LogisticRegression(max_iter=1000)),
])

minmax_pipeline = Pipeline([
    ('scaler', MinMaxScaler()),
    ('model', LogisticRegression(max_iter=1000)),
])

print(standard_pipeline)
print(robust_pipeline)
print(minmax_pipeline)
1 file · python Explain with highlit

Not every model cares about scale, but enough of them do that I keep scaling explicit. Linear models, SVMs, neural nets, and distance-based methods all benefit from well-behaved inputs. I prefer putting scalers inside the pipeline so train and inference paths cannot drift apart.

Share this code

Here's the card — post it anywhere.

Scaling and normalization choices for different model families — share card
Link copied