2.1.8 · D1Data Preprocessing & Feature Engineering

Foundations — Binning and discretization

2,947 words13 min readBack to topic

Everything in the parent note — bin edges , the mapping , entropy , quantiles — is built from a small pile of ideas. This page assumes you know nothing and constructs each symbol in order, so that by the end you can read the parent note without ever meeting an unexplained squiggle.


1. The number line — where all values live

Before anything, picture a ruler that never ends. Every price, temperature, age or salary is a single dot sitting somewhere on this ruler. Small values sit to the left, large ones to the right.

The opposite is a discrete variable: it can only be one of a few separate values, like "cheap / mid / premium". Those are separate marks with nothing in between.


2. The symbol — one data value

We need a short name for "a value on the ruler". Mathematicians write it .

When we have many values (a whole dataset), we write . The little number below (the subscript) is just an ID tag: is "the third data point". It is not multiplication or a power.


3. The symbol — how many values


4. min, max, and range — the two ends and the span

To chop the ruler we first need to know where the data starts and stops.


5. The symbol — how many bins


6. Bin edges — the cut marks

Now we actually chop. Each cut on the ruler is an edge, written .

The two outer edges are pinned to the data. For binning to actually cover every data value, we fix the first and last edges to the ends of the data: This guarantees no value falls off the left of the first box or the right of the last box — every dot lands somewhere.

Now, why the mixed bracket — square on the left, round on the right? That notation answers a real question: when a value lands exactly on a cut-line, which box does it go in?


7. The mapping — the binning machine itself

Now we can read the parent's central formula.

Read: "the label of is , whenever lands in box " — using the last-box fix from Section 6 so that maps to .


8. Quantiles and — cuts by headcount, not by distance

Equal-width binning (Section 5–6) cuts by distance. But the parent's second strategy cuts by headcount — same number of dots per box. That needs the idea of a quantile.

The parent writes sorted data as . The parentheses around the subscript — mean "the 3rd smallest value after sorting", not "data point 3". Sorting is required because you cannot count "the third of the way along" until the dots are in order.


9. Entropy — measuring "how much detail is left"

The parent's trade-off section uses , called entropy. You do not need to compute it here — you need the picture and a clear sense of what is.

Here uses the distribution of the raw values , while uses the distribution of the bin labels (there are only of them, so far fewer probabilities to sum). The parent's information loss is simply the difference of these two entropies: "detail before binning minus detail after" — always non-negative, because grouping can only reduce uncertainty, never add it.


10. How it all connects

number line + continuous variable

x a single value

n how many values

min max and range R

k number of bins

bin edges b_i

half open interval brackets

the mapping f of x

quantile Q and sorted data

probability p_i then entropy H

Binning and discretization

Each idea earns its place: you cannot state without min and max; you cannot place edges without and ; you cannot read without the half-open bracket rule (and the last-box fix); you cannot balance box headcounts without quantiles; and you cannot judge the trade-off without probabilities and entropy.


Where this leads

Once these symbols are solid, the parent topic connects outward:


Equipment checklist

Test yourself — say the answer aloud, then reveal.

What does a continuous variable let you do that a discrete one does not?
Take any value in a range, with no gaps between two values.
What does mean, and how is it different from ?
= data point number 3 (a name tag); = multiplied by itself three times (a power).
What does count?
The number of data points on the ruler.
Write the formula for range .
.
What dial does control?
The number of bins/boxes you cut the ruler into.
How many edges do you need for bins, and why?
edges — like a fence with gaps needing posts.
What two values must the outer edges and equal, and why?
and , so every data value is covered.
Write the equal-width formula for edge .
.
In , which end is included and which excluded?
is included (square bracket), is excluded (round bracket).
If every bin is , which single value falls into no box, and how is it fixed?
; fix it by closing the last bin on both ends, .
What happens to a new value or ?
It is clipped to the nearest end box (box 1 or box ), so accepts all of .
Read aloud: .
A function taking any real number and returning one box label from 1 to .
What question does answer?
Below which value do a fraction of the sorted dots sit?
Why must data be sorted before taking quantiles?
You cannot find "a fraction of the way along" until the dots are in order.
What is , and what do all the add up to?
The probability (fraction of dots) in bucket ; they sum to .
Write the Shannon entropy formula.
.
In words, what is ?
Detail before binning minus detail after — the information thrown away (always ).
What does literally count?
How many times you must double 1 to reach .