2.1.2 · D3Data Preprocessing & Feature Engineering

Worked examples — Handling missing values (deletion, imputation strategies)

2,742 words12 min readBack to topic

This page is the worked-example companion to Handling Missing Values. The parent note gave you the formulas. Here we grind through every kind of situation a missing-value problem can be — different missingness mechanisms, tiny toy tables, degenerate inputs (all-missing columns, single neighbour), time-series gaps, and an exam-style trap.

Before we compute anything, one promise: no symbol appears before it is earned. If you have not yet read the parent, that is fine — every term is rebuilt below in plain words with a picture.


Vocabulary rebuilt from zero


The scenario matrix

Every missing-value problem falls into one of these cells. Our examples below cover all of them — each example is labelled with the cell it hits.

Cell Data shape Mechanism Method that fits Trap to expose
C1 Numeric, symmetric MCAR Mean variance shrinks
C2 Numeric, skewed / outlier MCAR Median mean gets pulled
C3 Two rows survive MAR Listwise deletion bias by deletion
C4 Feature correlated with another MAR KNN / conditional mean breaks correlation
C5 Time series, short gap MCAR-ish Forward fill flat during a trend
C6 Categorical MCAR Mode mean is meaningless
C7 Degenerate: whole column NaN drop column no value to borrow
C8 Degenerate: only 1 neighbour MAR KNN with K=1 collapses to copy
C9 Real-world word problem MAR choose + justify reasoning, not formula
C10 Exam twist: leakage MAR fit on train only leakage

Ten cells. Ten examples. Let's go.


C1 — Mean imputation, symmetric data (MCAR)


C2 — Median beats mean when an outlier lurks (MCAR)


C3 — Listwise deletion and its hidden bias (MAR)


C4 — KNN imputation respects correlation (MAR)



C6 — Mode imputation for a categorical column (MCAR)


C7 — Degenerate case: the whole column is missing


C8 — Degenerate case: only one neighbour ()


C9 — Real-world word problem (MAR): the survey trap


C10 — Exam twist: imputation must not leak (MAR)


Recall

Recall Why does mean imputation shrink variance?

Because the fill value sits exactly at the mean, adding zero distance-from-mean, which dilutes the average squared distance. ::: A value at the balance point contributes 0 to the spread, so average spread drops.

Recall Under MAR, why is deletion biased but conditional imputation not?

Deletion removes rows non-randomly (e.g. the young), skewing the sample. ::: Conditioning on the observed cause (Age) reconstructs the missing values honestly.

Recall With

, KNN imputation equals what? A direct copy of the single nearest neighbour's value (weights cancel). ::: .

Recall On which set do you compute an imputation statistic to avoid leakage?

The training set only, then apply it to both train and test. ::: Fit on train, transform everywhere.

Related: Outlier handling · Feature scaling · Parent: Handling Missing Values.