2.1.8 · D3Data Preprocessing & Feature Engineering

Worked examples — Binning and discretization

3,171 words14 min readBack to topic

This page is the "throw everything at it" companion to Binning and discretization. The parent note built the three strategies (equal-width, equal-frequency, custom). Here we grind through every case class binning can hit — skewed data, ties, outliers, zero-range degenerate input, negative numbers, edge-boundary points, and a couple of exam twists — so that when you meet one in the wild you have already seen its cousin.

Before we start, three symbols we will lean on constantly:

Figure — Binning and discretization

The scenario matrix

Every worked example below is tagged with the cell it covers. The goal: no empty cells.

Cell Case class What could break Covered by
A Equal-width, well-spread data nothing — baseline Ex 1
B Equal-width, right-skewed + outlier empty middle bins Ex 2
C Equal-frequency on the same skewed data interpolation between points Ex 3
D Point sitting exactly on an edge double-membership / off-by-one Ex 4
E Negative values + zero crossing sign handling, min < 0 Ex 5
F Degenerate: all values identical () divide-by-zero in width Ex 6
G Ties at a quantile boundary equal-frequency can't split Ex 7
H Word problem: custom domain bins choosing edges from meaning Ex 8
I Exam twist: choose via Sturges log rounding, off-by-one Ex 9
J Limiting behaviour: and total loss vs. no compression Ex 10

Worked examples

Figure — Binning and discretization
Figure — Binning and discretization

Recall Quick self-check

On which side of an edge does a point that equals fall? ::: Into the bin to its right, — left edge included, right excluded. Why does equal-width leave empty middle bins on skewed data? ::: A far outlier stretches , so the width is huge and the sparse middle range catches no points (Ex 2). Why can't equal-frequency always give equal counts? ::: An edge cannot split a run of identical (tied) values, so all ties share one bin (Ex 7, Ex 10). What is the width formula's output when all values are equal? ::: ; degenerate, must be handled as a single bin (Ex 6). What does tell you about a binned feature? ::: It has only one label — it is constant and carries no information, so drop it. Sturges' bin count for ? ::: (or with ceiling rounding).

Related dives: outliers that these bins tame connect to handling missing/anomalous data, and once binned you can multiply bin labels together as feature interactions.