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.

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.

Overview

Missingness can be MCAR (unrelated to observed or missing values), MAR (explained by observed variables), or MNAR (depends on the missing value or another unobserved factor). These describe assumptions about how data went missing, not facts confirmed by the incomplete table alone.

In this guide
  1. Start with why the value is absent
  2. Compare the strategies
  3. Fit preprocessing without leakage
  4. Common mistakes with Handling Missing Values
  5. Sources and next steps

The cause matters. If high earners skip an income question, filling every gap with the median hides a potentially useful pattern. If a sensor failed randomly for a few readings, deleting those rows may be harmless. Ask how the data was collected and whether missingness will occur the same way at prediction time.

Compare the strategies

Use Strategy in the visualizer to compare Drop the rows, Fill with the mean, Fill with the median, and Median + missing indicator. The plot uses a deliberately skewed example: dropping the large values changes the remaining sample, while a constant fill creates a pile-up and pulls the distribution toward the fill value.

MethodUseful whenMain limitation
Drop rowsFew rows are missing and complete cases are representativeCan waste data or bias results if absence is systematic
Mean imputationA numeric feature is roughly symmetric and a simple baseline is neededSensitive to outliers; understates variation
Median imputationA numeric feature is skewed or has outliersCreates repeated values and can weaken relationships
Imputation plus indicatorThe fact that a value is missing may predict the outcomeAdds a feature; does not recover the missing value
Model-based imputationRelationships among features are useful and complexity is justifiedMore assumptions, tuning, and leakage risk

For example, if a feature has observed values 2, 3, 4, 5, 40 and two missing entries, mean imputation inserts 10.8 twice and shifts the feature upward; median imputation inserts 4 twice. Neither estimates the two unknown values exactly. An indicator adds a separate 1 for originally missing rows, letting a model learn whether absence itself is informative.

Fit preprocessing without leakage

Split the data first. Fit an imputer on training data, then apply that fitted transformation to validation and test data. In cross-validation, fit it anew inside each training fold. A scikit-learn Pipeline keeps that order. Fitting the fill value on the full dataset lets held-out rows influence training.

Some tree implementations can route missing values directly. That can be a good option when supported by the model and the missingness pattern is represented in training; compare it against an imputation baseline on held-out data.

Common mistakes with Handling Missing Values

  • Filling with zero automatically: zero may be a valid measurement.
  • Dropping every incomplete row across many columns: small per-column gaps can remove a large share of records.
  • Assuming an indicator always helps: validate it; missingness may be noise or may change after deployment.
  • Using a complex imputer without a reason: begin with a simple pipeline and measure whether added complexity improves the task.

Sources and next steps

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

Learn four strategies for handling missing data. Watch each method change predictions on the same dataset and pick the one that fits.

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.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.