Binning, and what it throws away
Cut a numeric column by width, by quantile or at chosen breaks, and weigh that against the information you lose.
You will be able to
- Tell equal-width bins from quantile bins
- Explain the cost of dichotomizing a measurement
- Name the situations where bins are worth it
Read first
Three ways to cut
ReadThe Bin step in Prepare cuts a numeric column three ways:
- Equal width gives bins of the same range. A skewed column leaves most bins nearly empty.
- Quantiles (equal counts) gives bins of the same size. Their ranges vary, and ties can complicate the edges.
- Custom breaks cut at values that mean something, such as a clinical threshold.
Bins change the picture
ExploreDrag the bin count and watch the same data change character. Too few bins hide structure, and too many show noise.
AExplorable · Bins and bandwidth
How do histogram bins and smoothing bandwidth change the story the same data tell?
How many humps does the population really have?
Bars: histogram with your bins. Line: kernel density with your bandwidth.
Commit your prediction above to see the result
Make your prediction to see the result.
If a chart can change this much, so can any analysis built on the bins.
What does a cut cost?
PredictPredict first
You split a normally distributed measurement at its median into "high" and "low", then test its relationship with an outcome. Compared with using the measurement itself, what does that cost?
Reveal
The answer: About as much as losing a third of your data
About a third of your effective sample, under normality (Cohen, 1983; MacCallum et al., 2002). Someone slightly over the line is treated as different from someone slightly under it, and as identical to someone far above it.
When bins earn their place
ReadBins are worth considering when:
- the cut point has real meaning, such as a legal age or a clinical threshold, and that's the question you are asking
- a report's readers need a small table
- you're building a scorecard that has to be used by hand
They are a poor default for testing or prediction. Keep the number, and use a spline or a tree if the relationship isn't straight.
Choosing a cut after seeing the outcome
ReflectTrying several cut points and keeping the one with the smallest p-value is a form of p-hacking. Each try is another chance for noise to look like signal.
Has a threshold in your work been chosen because of what it does to the result? If you need a cut, write down the reason before you look at the outcome.
In code
Readimport pandas as pd
ages = pd.Series([23, 35, 47, 52, 61, 70, 18, 44])
width = pd.cut(ages, bins=3)
counts = pd.qcut(ages, q=3)
chosen = pd.cut(ages, bins=[0, 18, 65, 120], labels=["minor", "adult", "senior"])ages <- c(23, 35, 47, 52, 61, 70, 18, 44)
width <- cut(ages, breaks = 3)
counts <- cut(ages, breaks = quantile(ages, 0:3 / 3), include.lowest = TRUE)
chosen <- cut(ages, breaks = c(0, 18, 65, 120), labels = c("minor", "adult", "senior"))Check your understanding
CheckpointCheck yourself
Income is heavily right-skewed. You make four equal-width bins. What do you expect?
Show the answer
Answer: b
The long tail stretches the range, so equal widths put nearly everyone in the first bin. Quantile bins give equal counts instead.
Check yourself
Which is the best reason to bin age at 65?
Show the answer
Answer: a
A cut with outside meaning answers a defined question. A cut chosen by its p-value overstates the evidence.
Recap
Recap- Equal width, quantiles and custom breaks answer different needs.
- Dichotomizing a measurement can cost about a third of your effective sample.
- Cut for a reason you can state before seeing the outcome.
Concepts in this lesson