Handling Missing Values

Drop them, fill them with the mean, fill them with the median — and watch what each choice does to the distribution it came from.

Overview

Why, before what

Every tutorial answers "what do I do about NaN". The question that decides the answer is why the value is absent, and there are three cases with three different consequences.

Missing completely at random. The absence has nothing to do with anything. A form field lost to a transmission error. This is the harmless case, and it is rare.

Missing at random. The absence depends on other columns you have. Younger respondents skip the income question more often, and you have their age. This is recoverable, because the information needed to model the absence is present.

Missing not at random. The absence depends on the missing value itself. High earners decline to state their income. This is the hard case, and it is also the common one.

The data in the visualisation is deliberately the third kind: the largest values are the ones that failed. Look at the two histograms. The grey distribution is what existed; the orange one is what each strategy leaves you with.

Handling Missing Values

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

The first question is never which strategy. It is why the values are missing.
Mean or median imputation puts a spike at one value, which shrinks the variance and weakens every correlation the column had.
Dropping rows is only safe when the missingness is unrelated to anything you care about. It rarely is.
A missing-value indicator column keeps the fact that it was missing, which is often the most predictive thing about it.

Handling Missing Values

Four strategies, and why choosing between them starts with a question about the world rather than about the data.

Drop the rows

Select Drop. The right tail is gone, and the mean in the readout has fallen well below the true mean.

Nothing about dropping is wrong in principle. It is unbiased when the values are missing completely at random, and with a handful of affected rows out of many it is a perfectly reasonable choice.

Here it is a disaster, because the rows removed are not a random sample — they are the large ones. Every statistic computed afterwards describes a population that excludes them.

The other cost is arithmetic. Dropping any row with any missing value across ten columns each 5% missing removes about 40% of the data, even though 95% of every individual column is present.

Fill with the mean, or the median

Select Mean, then Median. Both put a spike at a single value in the middle of the distribution, and both leave the mean below the truth.

They differ in robustness. The mean is dragged by extreme values; the median is not. For a skewed column — income, house prices, time-to-event — the median is the better default, and for a roughly symmetric column the two are close enough that it rarely matters.

What neither can do is invent information. Imputation makes the dataset rectangular so the model will accept it. It does not restore what was lost, and it introduces two distortions worth knowing:

  • Variance shrinks. A pile of identical values has none.
  • Correlations weaken. The imputed rows carry no relationship to any other column, so they dilute every relationship the column really had.

Keep the fact that it was missing

Select Median + indicator. The imputation is the same; what changes is that a second column is added, holding 1 where the value was missing and 0 otherwise.

This is usually the best simple answer, because in the third case — missing not at random — the absence is itself informative. If high earners decline to answer, then "declined to answer" predicts high earnings, and plain imputation throws that signal away while an indicator preserves it.

It costs one column and no assumptions. It is hard to do worse than the alternatives with it.

StrategyKeeps rowsKeeps distributionKeeps the signal in absence
Dropnoonly if MCARno
Meanyesno, spike at centreno
Medianyesno, spike at centreno
Median + indicatoryesnoyes
Model-based (kNN, MICE)yesbetteronly with an indicator

More sophisticated options

k-NN imputation fills a value from similar rows. MICE models each column from the others, iteratively. Both preserve relationships better than a constant and both cost far more computation, and both must be fitted on training data only — they are models, so imputing before the split is [leakage](data_leakage.html).

Also worth knowing: some models handle missing values natively. LightGBM and XGBoost learn a default direction at each split for rows whose value is absent, which is frequently better than anything you would impute by hand.

Where it goes wrong

Imputing before the split. The column mean is computed from the test rows too.

Filling with zero without thinking. Zero is a real value in most columns. A temperature of zero is not a missing temperature.

Dropping rows across many columns at once. A little missingness everywhere removes a lot of data.

Never checking why. Ten minutes finding out what caused the gap is worth more than any choice of strategy.

Check yourself

0 of 3

Answer without scrolling back up.

  1. What does 'missing not at random' mean?

  2. Why does mean imputation weaken a column's correlations?

  3. Why add a missing-value indicator column?

Cheat sheet

Handling Missing Values

Every tutorial answers "what do I do about NaN". The question that decides the answer is why the value is absent, and there are three cases with three different consequences.

MACHINE LEARNING · vizlearn.in/machine_learning/handling_missing_values.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.