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 intervals of identical width. Given values with minimum and maximum , the bin width is
and bin covers the interval . 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 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 bins both ways.
Equal-width. The width is :
| Bin | Range | Count |
|---|---|---|
| 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 values per bin:
| Bin | Range | Count |
|---|---|---|
| 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.
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 ; equal-frequency binning places about values in each bin; supervised binning chooses boundaries using the target. On skewed data, equal-frequency almost always gives the more informative discretization.