C.W.K.
Stream
Lesson 07 of 08 · published

Feature Scaling and Encoding

~28 min · preprocessing, scaling, encoding

Level 0Scout
0 XP0/48 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

When scaling matters

Linear models, distance-based models (kNN, k-means), and neural networks need scaled features. Tree-based models do not — they only care about ordering. Use StandardScaler for symmetric distributions, RobustScaler when outliers are present, and MinMaxScaler when bounded inputs are required (some neural nets, image pipelines).

Encoding categoricals

  • One-hot for low-cardinality (under ~30 categories), works with any model.
  • Ordinal when the order is meaningful and the model is tree-based.
  • Target encoding when cardinality is high and the model is tree-based; always apply inside CV folds.
  • Hashing when memory is the constraint and you can tolerate collisions.

Pipeline placement

Scalers and encoders belong inside the pipeline, fit on training only, applied to validation and production identically. Keep the column transformer as the single entrypoint for raw rows; never preprocess in a notebook cell that is not in the pipeline.

Code

Mixed numeric + categorical preprocessing·python
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline

numeric_pipe = Pipeline([
    ("impute", SimpleImputer(strategy="median")),
    ("scale", StandardScaler()),
])
categorical_pipe = Pipeline([
    ("impute", SimpleImputer(strategy="most_frequent")),
    ("onehot", OneHotEncoder(handle_unknown="ignore")),
])
preprocess = ColumnTransformer([
    ("num", numeric_pipe, numeric_cols),
    ("cat", categorical_pipe, categorical_cols),
])
Target encoding done safely with category_encoders·python
from category_encoders import TargetEncoder

te = TargetEncoder(cols=["merchant_id", "city"])  # fit per CV fold
X_tr_enc = te.fit_transform(X_tr, y_tr)
X_val_enc = te.transform(X_val)

External links

Exercise

For your dataset, list every numeric and categorical column. Choose a scaling/encoding strategy per column. Build a ColumnTransformer that wraps it all and verify the output shape.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.