Contents
Figure 1: Grouping is tidiness with a bill attached — the trick is knowing when the bill is worth paying
Grouping numbers into bins: when it rescues a model, when it wrecks one
Turning "age 37" into "age 30–40" feels tidy. It's easy to explain, it hides noise, and it makes charts cleaner. It also throws information away, and sometimes that's exactly the right trade.
Binning was tested against raw numbers and smoother alternatives on data where the truth was known in advance. It rescued a linear model in some cases, wrecked it in others, and had one cost that matches a famous number almost to the decimal.
What Binning Actually Does
Binning (or discretization) replaces a continuous number with the interval it falls in. Scikit-learn's KBinsDiscretizer offers three strategies:
- Uniform: every bin has the same width.
- Quantile: every bin holds the same number of points. This is the default.
- K-means: bin boundaries come from a 1-D k-means clustering.
You can encode the result as one-hot columns or as ordinal bin numbers. The defaults are n_bins=5, encode='onehot', strategy='quantile'. The edges were verified by hand: uniform edges equal linspace(min, max, k+1), and quantile edges equal the matching percentiles:
from sklearn.preprocessing import KBinsDiscretizer
enc = KBinsDiscretizer(n_bins=10, strategy="quantile", encode="ordinal")
X_binned = enc.fit_transform(X_train) # edges learned from training data only
X_test_binned = enc.transform(X_test) # out-of-range lands in the end bins
Pick the Strategy for Your Distribution
Strategy matters most when data is skewed. 1,000 lognormal values were binned into 10 bins and counted:
| Strategy | Smallest bin | Largest bin | Empty bins |
|---|---|---|---|
| Uniform | 0 | 800 | 2 |
| Quantile | 100 | 100 | 0 |
| K-means | 1 | 490 | 0 |
Uniform bins dumped 800 of 1,000 values into a single bin and left two bins empty. Quantile bins were perfectly balanced by construction. K-means sat in between, with one bin holding a single value.
FYI: scikit-learn's docs note that empty bins can produce constant features under one-hot encoding, and suggest VarianceThreshold to remove them. For skewed data, start with quantile — the same skew instinct as the transform guide.
The Price of Grouping: How Much Signal Survives?
Here's the number that matters. A simulation of y = x + noise, tuned so a model using the raw x gets R² = 0.5, then grouped x and measured test R² (50 datasets):
| Grouping | Test R² | Share of raw R² kept |
|---|---|---|
| Raw x | 0.503 | 1.00 |
| Median split (2 groups) | 0.321 | 0.64 |
| 3 groups | 0.397 | 0.79 |
| 5 groups | 0.451 | 0.90 |
| 10 groups | 0.481 | 0.96 |
A median split kept 64% of the explanatory power. Theory predicts 2/π ≈ 0.637 for a normal predictor, so the simulation lands almost exactly on the textbook value. Cutting a continuous variable in half discards over a third of what it could explain. IMO, that's the best single argument against casual dichotomizing ("high" vs "low").
When Bins Win and When They Lose
Now the main question: does a linear model do better with bins? It depends entirely on the true shape. A feature x in [0, 1] with 200 training rows and 20 datasets:
| Setup | sin, n=200 | sin, n=5000 | line, n=200 | line, n=5000 |
|---|---|---|---|---|
| Raw x (line) | 0.512 | 0.515 | 0.786 | 0.789 |
| 3 bins | 0.581 | 0.578 | 0.694 | 0.703 |
| 5 bins | 0.732 | 0.743 | 0.749 | 0.757 |
| 10 bins | 0.807 | 0.821 | 0.767 | 0.781 |
| 20 bins | 0.820 | 0.841 | 0.762 | 0.787 |
| 50 bins | 0.806 | 0.846 | 0.733 | 0.787 |
| Best possible | 0.847 | 0.847 | 0.787 | 0.787 |
Read it in two halves:
- Wavy truth (sin): a straight line manages 0.51, while 10 to 20 bins climb to about 0.81–0.84. Binning lets a linear model bend.
- Truly linear truth: the raw line already hits the ceiling (0.786), and every binning option scores lower or at best ties. At n=200, 50 bins fell to 0.733, because each bin has only about 4 points and the estimate gets noisy. With 5,000 rows the penalty mostly disappears.
The best bin count depends on both the shape and your sample size. Too few bins can't follow the curve, and too many chase noise.
Bins Versus Smoother Tools
Binning produces a staircase. Scikit-learn has smoother ways to make a linear model bend: PolynomialFeatures and SplineTransformer. Compared on three truths (n=200, test R², 20 datasets):
| Method | U-shape | Smooth wave | Sharp step at 0.6 |
|---|---|---|---|
| Raw x (line) | -0.013 | 0.513 | 0.656 |
| 10 quantile bins | 0.738 | 0.806 | 0.850 |
| Polynomial, degree 3 | 0.792 | 0.836 | 0.761 |
| Spline (6 knots, degree 3) | 0.788 | 0.840 | 0.827 |
| HistGradientBoosting on raw x | 0.745 | 0.813 | 0.898 |
| Best possible | 0.798 | 0.847 | 0.914 |
The U-shape shows why you need something: a straight line scored -0.013, worse than guessing the average. After that:
- Smooth truths (U-shape, wave): polynomial and spline beat bins slightly, and they use fewer columns.
- The sharp step: bins beat the polynomial (0.850 vs 0.761), and the tree model won outright.
One honest caveat on that last column: the step sits at x=0.6, and with 10 quantile bins on uniform data, a bin edge falls near 0.6 — that flatters the bins. A step that falls mid-bin would hurt them. The tree model doesn't need such luck, since it finds its own split.
Practical Gotchas
- Out-of-range values land in the end bins. Fit on [0, 1], then transform -3 and 5: both map to the first and last bin. Scikit-learn's docs explain why — the outer bin edges extend to ±infinity at transform time. Reasonable, but a wild new value will quietly look like an ordinary extreme.
- Splines behave differently by default. With
extrapolation='constant', a value of 5 produced the same row as the training maximum of 1. You can choose'linear','continue', or'error'instead. - Fit bins on training data only. Bin edges are learned parameters, so keep the discretizer in your pipeline like any other transformer — the rule the preprocessing guide applies to every step.
- Trees don't need your bins. Gradient boosting scored as well as or better than binned linear models on all three shapes, using the raw feature. Pre-binning for a tree mostly throws away options.
A real-world version of that last gotcha: a pricing feature was binned into tidy revenue tiers for an executive dashboard, and the tiers looked harmless — until churn analysis kept missing a cancellation spike that sat right in the middle of one bucket. The bin had swallowed a cliff at a price point that mattered, and no model built on the tiers could see it. The raw column went back into the pipeline, and the tiers stayed in the dashboard only. Binning for presentation is fine; binning away the resolution you'll need later is the expensive version.
When Should You Bin?
- Linear model, curved relationship: try splines or polynomials first, then bins. Bins are the more interpretable choice if you need to say "customers aged 30–40 do X."
- Thresholds are real: if a policy or physical rule creates a step (tax brackets, dosage cutoffs), bins fit the structure.
- Skewed data: use quantile bins to avoid empty or lopsided groups.
- Tree models: don't bother. They already split on thresholds.
- Never dichotomize a continuous predictor just for convenience — the median split cost about 36% of the signal.
And remember bins obey the same range rules as scaling: edges are fit on training data, and production values outside the range slide into the end bins without complaint.
Common Pitfalls
- Median-splitting a continuous variable for tidiness — 36% of the signal gone by theory, confirmed by simulation.
- Uniform bins on skewed data — 800 of 1,000 values in one bin and two empty bins.
- Choosing bin count without checking sample size — 50 bins on 200 rows means ~4 points per bin and noisy estimates.
- Letting a lucky bin edge take credit — a step landing on a boundary flatters bins; test with a shifted step.
- Pre-binning before a tree model — it already does that, with better boundaries.
- Fitting bins on the full dataset — edges are learned parameters; keep them in the pipeline.
- Binning away a threshold you'll need later — presentation bins don't belong in the model.
Best Book to Read
| Cover | Book | Description | Get it |
|---|---|---|---|
![]() |
An Introduction to Statistical Learning | why information loss from grouping is quantifiable, and when splines beat piecewise constants in practice. | View on Amazon |
![]() |
Python Feature Engineering Cookbook | recipes for equal-width, equal-frequency, and tree-based discretization inside leakage-safe pipelines. | View on Amazon |
![]() |
Feature Engineering for Machine Learning | binning treated as a modeling decision with explicit trade-offs, alongside the encoding choices that follow it. | View on Amazon |
![]() |
Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow | where discretization, polynomials, and splines sit in the broader toolkit, with scikit-learn APIs in context. | View on Amazon |
As an Amazon Associate I earn from qualifying purchases. Links on this page are affiliate links, and I may earn a commission at no extra cost to you.
Unlock AI That Actually Works
Get lifetime access to GPT-6 Astra, Claude Fable 5.1, Gemini 3.5, Grok 4.5, and more — all in one platform. Build websites, apps, videos, content, and digital products from a single command. No monthly fees. No tool-hopping.
Click here to get GPTAstra Max now — one-time payment, lifetime access.
Frequently Asked Questions
When should you bin continuous variables?
When the relationship is curved or stepped and you need interpretability: binning let a linear model follow a sine-shaped truth from 0.51 to 0.84 test R² in these tests, and bins beat polynomials on a sharp step (0.850 vs 0.761). Thresholds that are real — tax brackets, dosage cutoffs, policy rules — also favor bins because the structure itself is a step. Bin with quantile strategy on skewed data, keep the discretizer inside the pipeline, and don't bin for tree models: they already split on thresholds.
How many bins should I use?
It depends on shape and sample size. In the bin-count sweep, a wavy truth climbed from 0.58 with 3 bins to 0.81–0.84 with 10–20 bins, while a truly linear truth peaked at the raw feature and lost accuracy once bins got numerous: with 200 rows and 50 bins each bin holds about 4 points, and test R² fell to 0.733. Too few bins can't follow the curve, too many chase noise — start around 5–10, then check cross-validated performance.
What is the difference between uniform, quantile, and k-means binning?
Uniform gives every bin the same width, quantile (scikit-learn's default) gives every bin the same number of points, and k-means derives boundaries from 1-D clustering. On 1,000 lognormal values split into 10 bins, uniform dumped 800 values into one bin and left two bins empty; quantile was perfectly balanced (100/100); k-means sat in between. For skewed data, start with quantile — and note that empty bins can become constant one-hot features, which VarianceThreshold can remove.
Is dichotomizing a continuous variable a good idea?
Usually not. In the simulation of y = x + noise, a median split kept only 64% of the raw feature's explanatory power (test R² 0.321 vs 0.503) — and theory predicts 2/π ≈ 0.637 for a normal predictor, so the simulation lands almost exactly on the textbook value. Cutting a continuous variable in half discards over a third of what it could explain. Only do it when a real threshold exists, not for tidiness.
Wrapping This Up
Binning can help a linear model bend, and it matches real thresholds well. But it throws information away, and it can hurt badly when the truth is simple. Smooth alternatives like splines often do as well with fewer columns, and tree models don't need it at all.
Plot your feature against the target, decide whether the pattern is smooth, steep, or linear, and let that choose your method.



