Skip to content
Saed Sayad

Binning

Binning (discretization) turns numerical variables into categorical ones. Compare equal-width and equal-frequency binning with formulas and a worked example.

4 min read · Updated August 8, 2026

Binning — or discretization — is the process of transforming a numerical variable into a categorical counterpart: turning Age into ranges such as 20–39, 40–59, and 60–79. Modeling methods based on frequency tables (decision trees, OneR, Naive Bayes) usually need numerical variables discretized before they can use them.

Binning can also improve a model’s accuracy by reducing noise or non-linearity, and it makes outliers, invalid values, and missing values of a numerical variable easy to spot. There are two broad types: unsupervised binning, which looks only at the variable itself, and supervised binning, which uses the target.

Unsupervised binning

Equal-width binning

Equal-width binning divides the variable’s range into kk intervals of identical width. Given nn values with minimum xminx_{\min} and maximum xmaxx_{\max}, the bin width is

w=xmaxxminkw = \frac{x_{\max} - x_{\min}}{k}

and bin ii covers the interval [xmin+(i1)w,  xmin+iw)[\,x_{\min} + (i-1)\,w,\; x_{\min} + i\,w\,). The method is simple, but it is sensitive to skew and outliers: a few extreme values stretch the range and leave most bins nearly empty.

Equal-frequency binning

Equal-frequency (or equal-depth) binning chooses bin boundaries so that each bin holds approximately the same number of values — about n/kn / k per bin. Bin widths vary to match the data’s density: narrow where values crowd together, wide where they are sparse. This makes it robust to skew, though identical values can force uneven counts.

Supervised binning

Supervised binning uses the target variable to place boundaries — typically by choosing split points that maximize information gain, the same entropy criterion a decision tree uses. It produces bins that are actually predictive, at the cost of a higher overfitting risk.

Worked example

Take 30 annual incomes (in $k), sorted:

21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 38, 39, 41, 43, 45, 48, 51, 55, 59, 64, 70, 78, 89, 104, 128

The distribution is right-skewed — most incomes sit under 42, with a long tail out to 128. Bin it into k=5k = 5 bins both ways.

Equal-width. The width is w=(12821)/5=21.4w = (128 - 21) / 5 = 21.4:

BinRangeCount
1[21.0, 42.4)18
2[42.4, 63.8)6
3[63.8, 85.2)3
4[85.2, 106.6)2
5[106.6, 128.0]1

Equal-frequency. With 30/5=630 / 5 = 6 values per bin:

BinRangeCount
1[21, 27]6
2[28, 33]6
3[34, 41]6
4[43, 59]6
5[64, 128]6

Try it: binning lab

Step through both methods on this dataset with the interactive widget — toggle equal-width versus equal-frequency and drag the bin count to watch the histogram reshape.

Static summary (30 incomes in $k, k = 5):

BinEqual-width range (w = 21.4)CountEqual-frequency rangeCount
1[21.0, 42.4)18[21, 27]6
2[42.4, 63.8)6[28, 33]6
3[63.8, 85.2)3[34, 41]6
4[85.2, 106.6)2[43, 59]6
5[106.6, 128.0]1[64, 128]6

Equal-width keeps intervals uniform, so the skew crams 18 of 30 values into the first bin. Equal-frequency keeps counts uniform — six values per bin — and lets the bin widths express the skew.

In practice

In scikit-learn, sklearn.preprocessing.KBinsDiscretizer implements both strategies (strategy="uniform" for equal-width, "quantile" for equal-frequency, plus "kmeans"), and pandas offers pd.cut and pd.qcut for quick one-off work. Tree-based models like XGBoost and LightGBM discretize internally — histogram boosting is essentially supervised binning at scale — so manual binning matters most for linear models and frequency-table methods. As with encoding, fit bin edges on the training data only, inside a Pipeline, to avoid leakage.

Common pitfalls

  • Equal-width on skewed data. One stretched range and nearly every value lands in a single bin.
  • Too many bins. Tiny bins overfit noise; too few bins destroy the signal. Start with 5–10 and validate.
  • Fitting bin edges on all the data. Boundaries computed before the train/test split leak information.
  • Discarding the order. Binned categories are ordinal — keep the ordering instead of treating bins as unrelated labels.

Summary

Binning converts numerical variables into categorical ones. Equal-width binning uses fixed intervals of width w=(xmaxxmin)/kw = (x_{\max} - x_{\min})/k; equal-frequency binning places about n/kn/k values in each bin; supervised binning chooses boundaries using the target. On skewed data, equal-frequency almost always gives the more informative discretization.