Binning, Discretization & Interaction Features
Create bins and interactions that capture non-linearity when models need help.
In plain words
Binning sorts numbers into groups like age bands, and interactions combine two columns, such as age times income, so a simple model can capture patterns that aren't just straight lines.
Think of it like…
Turning a smooth speedometer reading into traffic-light bands (slow, normal, fast) loses detail but makes rules easy to state, and noticing that rain matters mainly at night is an interaction: the effect of one thing depends on another.
Where you’ve seen it
A logistic regression for default risk uses age linearly and scores AUC 0.70. Default risk is actually U-shaped (young and very old customers are riskier), so the team bins age into five quantile bins (about 20% of customers each) and one-hot encodes them: AUC rises to 0.74. A spline basis on age gives 0.75, nearly the same with smoother behaviour. For income they use quantile bins as well, then add the ratio debt / income (an interaction capturing affordability), which becomes the strongest feature (AUC to 0.79). They keep the feature count in check: all pairwise products of 20 features would produce C(20, 2) = 190 interaction terms, and a full degree-2 polynomial expansion yields 20 originals + 20 squares + 190 products = 230 columns in total (degree 3 would give C(23, 3) − 1 = 1,770), so they select a handful of interactions from domain knowledge and use L1 regularisation. A gradient-boosted tree on raw features reaches 0.82 without any manual binning or interactions, a reminder that these techniques mainly help simpler models.
Words you’ll hear
- Equal-width vs quantile binning
- Bins of equal size in value versus bins containing equal numbers of rows.
- Interaction feature
- A feature built from several variables, such as a product or ratio.
- Polynomial features
- All products of features up to a given degree.
- Weight of evidence (WoE)
- Log-odds of the target within a bin, used in scorecard modelling.
Visual diagram
Theory & deep dive
Binning strategies
KBinsDiscretizer supports uniform (equal-width), quantile (equal-frequency) and k-means strategies, with encode="onehot" or "ordinal". Equal-width bins are simple but leave sparse bins when the data is skewed (most incomes fall in the first bin); quantile bins put roughly the same number of rows in each bin (20% each for five bins), which gives stable estimates but irregular widths. Domain-driven edges (age bands 18–25, 26–40, 41–65, 65+, tax brackets, clinical thresholds) improve interpretability and match business rules. In credit scoring, monotonic binning with weight of evidence (WoE = ln(share of events / share of non-events) in the bin) produces scorecards whose points are easy to audit.
Trade-offs: binning lets a linear model fit non-linear, non-monotonic effects (the U-shaped age risk), reduces the influence of outliers and handles missing values as their own bin; but it discards within-bin variation, creates abrupt jumps at edges and needs enough data per bin. Choose the number of bins by cross-validation (5 to 10 is common), fit bin edges on training data only and monitor drift in bin populations. Smooth alternatives, splines (SplineTransformer) and polynomial terms, capture curvature without arbitrary jumps and often match or beat binning in accuracy. Tree-based models learn their own thresholds, so manual binning rarely helps them.
Ages from 1 to 100 uniformly, 4 quantile bins: edges at 25.75, 50.5 and 75.25 so each bin holds 25% of rows. Equal-width bins would be 0–25, 25–50, 50–75, 75–100, which coincide here but diverge badly for skewed data such as income.
Interaction and polynomial features
An interaction lets one feature's effect depend on another: age × income, is_mobile × hour, or ratios such as debt/income, price/area, clicks/impressions, revenue/employee. Ratios and differences often encode domain concepts (affordability, density, change over time) better than raw inputs and are among the most powerful hand-made features. PolynomialFeatures(degree=2, interaction_only=True) generates all pairwise products automatically, but the count grows quickly: p features give p(p − 1)/2 pairwise products (190 for p = 20), a full degree-2 expansion yields p + p + p(p − 1)/2 = 230 columns in total (originals, squares and products), and degree 3 gives C(p + 3, 3) − 1 = 1,770 columns in total. With few rows this causes overfitting, collinearity and heavy computation.
Control the explosion: select interactions from domain knowledge or from analysis of model residuals and partial dependence; use L1 or elastic-net regularisation to prune; apply feature selection inside cross-validation; scale features before creating products (so one dominates less); and prefer tree-based or boosted models when you want interactions discovered automatically (they split on one feature then another). Check that interactions generalise: compare cross-validated performance with and without them and examine stability across folds and time. For interpretability, document each engineered feature and its meaning, since a coefficient on age × income is harder to explain than one on age.
p = 20. Pairwise products: 20 × 19 / 2 = 190. Degree-2 polynomial with include_bias=False: 20 originals + 20 squares + 190 products = 230 columns in total. Degree 3: C(23, 3) − 1 = 1,771 − 1 = 1,770 columns in total.
How it works, step by step
Step 1 of 41. Find non-linear and conditional effects
Plot target rate by feature bins, partial dependence and residuals to see curvature and interactions.
- Compare
- with a GBDT baseline
When to use it & trade-offs
What it is
Binning (discretisation) converts a continuous feature into ordered categories, using equal-width bins, equal-frequency (quantile) bins, domain-defined cut points or supervised/monotonic bins (as in credit-risk scorecards with weight of evidence). Interaction features are products or ratios of features (age × income, price ÷ area, clicks ÷ impressions) that let a model represent effects that depend on combinations of variables. Both techniques help linear and generalised linear models capture non-linearity and interactions that they can't learn on their own; tree ensembles find splits and interactions automatically, so they benefit less. Both techniques add flexibility, and with it the risk of information loss (binning) or feature explosion and overfitting (interactions).
Why it matters: A plain linear model assumes each feature affects the outcome in a straight line and independently of the others. Real effects are curved (risk rises sharply after age 65), thresholded (a discount applies above $100) and conditional (an ad works for young users on mobile). Binning and interactions are low-tech ways to give simple, explainable models that flexibility, and they remain important in regulated settings where the model must be a scorecard or a logistic regression.
When to reach for it
- Scorecards and regulated models
- Linear and logistic regression baselines
- Domain ratios such as affordability and density
When to avoid or be careful
- Expanding all pairwise products on small datasets
- Fitting bin edges on the full data
- Binning for tree models without evidence of benefit
Ways to model non-linearity
Strengths: Handles non-monotonic effects and outliers; easy to explain.
Watch out: Loses within-bin detail; jumps at edges.
Strengths: Continuous and flexible.
Watch out: Less intuitive; risk of wild tails.
Strengths: Little manual feature work.
Watch out: Less transparent than a scorecard.
Interview signals & numbers to know
When this topic comes up
- “Risk isn't linear in age”
- Weight of evidence
- KBinsDiscretizer
- PolynomialFeatures
- Ratio features
Benchmarks & rules of thumb
- p(p − 1)/2
- pairwise interactions: 190 for 20 features
- 20 + 20 + 190 = 230
- total columns in a degree-2 expansion
- 0.70 → 0.74 → 0.79
- AUC with bins, then a ratio feature
Common mistakes & follow-ups
Common interview mistakes
- Choosing bin edges by looking at the test set.
- Creating hundreds of interactions with a few hundred rows.
- Forgetting to scale before forming products.
Likely interviewer follow-ups
- When would you bin a continuous variable?
- How do interaction features help a linear model?
- How do you avoid feature explosion?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Binning, Discretization & Interaction Features with data science interview Q&A and hands-on practice.