Building features
Turning raw data into columns a model can use: encoding, scaling, time features, and the most common way of fooling yourself.
The idea
A model sees columns of numbers. A date like 2026-08-25 means nothing to it. It
becomes useful as "Tuesday", "week 35", "three days before month end". Feature
engineering is the translation of reality into columns like those.
The usual transformations
| Raw value | Useful features |
|---|---|
| Date | Weekday, month, holiday, days since an event |
| Address | Region, distance to site, urban or rural |
| Text | Length, language, presence of key terms, embedding |
| Amount | Amount, logarithm, deviation from the customer's average |
| Category | One-hot, frequency, target encoding |
The pipeline that prevents leakage
import numpy as np, pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.ensemble import HistGradientBoostingClassifier
num = ["amount", "days_since_last_order"]
cat = ["region", "channel"]
prep = ColumnTransformer([
("num", Pipeline([("imp", SimpleImputer(strategy="median")),
("sc", StandardScaler())]), num),
("cat", OneHotEncoder(handle_unknown="ignore", min_frequency=20), cat),
])
# Everything inside one pipeline: the median and the category list are
# learned per fold from the training part only. That is what stops leakage.
model = Pipeline([("prep", prep),
("clf", HistGradientBoostingClassifier())])- Never compute the median across the whole dataset and split afterwards.
- Set
handle_unknown="ignore", otherwise production breaks on new categories. - Merge rare categories rather than creating hundreds of columns with three cases each.
- Encode cyclic quantities such as hour or month as sine and cosine, not as a number.
Smoothed target encoding
Without smoothing, a category with a single case gets exactly that case's target value and becomes a disguised copy of the label. Without cross-validation inside the encoding the same happens for larger categories, only more subtly.
Cyclic encoding
As a plain number, 23 and 0 sit as far apart as possible although one hour separates them. Irrelevant for tree methods, a systematic error for linear models and networks.
What actually matters on tabular data
Empirically, gradient boosting on well-built features beats deep networks on the same data in the majority of cases. The three most productive moves:
- Group aggregates. Mean, maximum and count per customer, site or month, each computed from the past only.
- Differences and ratios. Amount divided by that customer's usual amount says more than the amount itself.
- Time distances. Days since the last event, days until the deadline.
All three are prone to leakage along the time axis. The effort is not in the computation but in getting the windows right. See Data leakage.
Related courses and sources
Kaggle Learn
Short units with runnable notebooks, from Python through pandas to a first model. No installation, straight in the browser.
For the very first steps with no installation, straight in the browser.