Imputation strategies

Fill in missing values in a way that keeps the signal and does not leak.

Lesson 15 of 6610 minBeginner

You will learn to

  • Choose an imputation strategy per column
  • Explain how imputation shrinks variance
  • Recognise when missingness is itself informative and keep that signal

Before this lesson

Intuition

A form with some blank boxes still has to be processed. Most models cannot accept a blank, so you must put something in it. This is imputation: replacing missing values with a plausible number or category.

The temptation is to treat it as housekeeping. It is a modelling decision, because whatever you write into the blank is something the model will believe. Three questions guide the choice:

  1. Why is it missing? If the reason is random, any sensible fill works. If the missingness depends on the value itself or on the outcome (people with a high income leave the income box empty), the blanks carry information. See Missing values.
  2. What type is the column? The mean and median need numbers. For categories, use the most frequent value or a new category named "missing".
  3. How much is missing? A column that is 60% empty has little to say. A guess for most rows may add more noise than signal.

Imputation is also a trade: it keeps all rows, but it replaces unknowns with guesses, and guesses are less variable than real data.

Picture it

Observed values: variance 8024681012After filling 5 missing rows with the mean: variance 4024681012
Five observed values and the same column after five missing rows are filled with the mean. The imputed points (highlighted) pile up on the mean and the spread shrinks.

The math

Common strategies for a numeric column are the mean xˉ\bar{x}, the median, and a constant such as 0. For a categorical column they are the most frequent value, and a constant such as "missing".

Why the mean shrinks the spread. Suppose nn values are observed with variance s2s^2 (using the population formula, sum of squares divided by nn), and kk more rows are missing and filled with the mean xˉ\bar{x}. The new rows have deviation 0, so the sum of squared deviations is unchanged, but it is divided by n+kn + k rows:

safter2=nn+k s2s^2_{\text{after}} = \frac{n}{n+k}\, s^2

With half of the rows missing (k=nk = n) the variance is halved. Standard errors and correlations computed afterwards are therefore too optimistic: the data looks more certain than it is.

Median versus mean: the mean minimises the squared error and is the right centre for symmetric data. The median minimises the absolute error and is robust to outliers and skew.

Worked example

Five observed values: 2, 4, 6, 8, 10. Mean 6, variance 8 (population formula). Add five missing rows and fill them with the mean 6.

  • Ten values now: 2, 4, 6, 8, 10, 6, 6, 6, 6, 6.
  • The sum of squared deviations is still 40, now divided by 10: variance 4, exactly 510×8\tfrac{5}{10}\times 8.

Next, a skewed column: salaries in thousands 30, 35, 40, 45, 50, 55, 60, 200. The mean is 64.4, the median 47.5. Seven of the eight people earn at most 60, so filling a missing salary with 64.4 puts a person above almost everyone else. The median, 47.5, is a much more typical value.

Lastly, informative missingness. Imagine a loan dataset where income is blank for applicants who did not want to disclose it, and these applicants default more often (illustrative). If you fill every blank with the median income, a non-disclosing applicant looks like an average one, and the signal is lost. Two remedies: add a 0/1 column "income was missing" next to the filled one; or, for categories, fill with a new value "missing" so the model can learn its meaning.

How mlflow does it

  • The step. In Pipeline Studio, the Fill missing values group of the Choose... list offers Fill missing: average, Fill missing: median, Fill missing: most common and Fill missing: fixed value (which asks for the value to use). Pick the strategy per column, or select several columns and use Choose for N columns; suggestions can be applied with Apply. As a safety net, Studio pipelines also fill any missing numeric or boolean value at prediction time with values learned on the training rows, but only an Imputation step lets you choose the strategy.
  • Numeric versus categorical. Mean and median apply to numeric columns only. Most frequent and constant also work for categories.
  • Recommendations. For numeric columns with missing values the recommender suggests median ("robust to outliers and skew"), and for categorical columns most_frequent, naming the columns and the share missing.
  • Fitted inside each fold. The fill values (means, medians, most frequent categories) are computed on the training rows of each fold, so the validation rows never influence them.
  • Missing-value flags. The Imputation step does not add a flag column. To keep informative missingness today, use strategy constant with a distinctive fill for categories, or prepare a flag column in your data before upload, using only information known at prediction time.
  • Where to look. The Missing tab of the data profile shows the missing rate per column and the columns affected.

Common mistakes

  • Always using the mean. It is pulled by outliers and meaningless for categories. Median for numbers and most frequent for categories are safer defaults.
  • Imputing before splitting. The fill value would use test rows. Make it a pipeline step so each fold computes its own.
  • Imputing a column that is mostly empty. A guess for 80% of the rows adds noise. Consider dropping the column or keeping only a "was present" flag.
  • Erasing informative blanks. If the missingness relates to the target, keep that signal as a flag or a separate category.
  • Imputing the target. Rows without a target cannot be used for training; remove them rather than inventing labels.
  • Trusting statistics after imputation. Variances and correlations are biased towards zero spread; judge the data's real uncertainty from the observed values.

Try it in mlflow

Add an imputation step

  1. Open Pipelines and pick a pipeline whose dataset has missing values.
  2. In the Columns part, find a column with gaps, press Choose... and, under Fill missing values, pick Fill missing: median (or average). Its chip shows Before and after on a real sample.
  3. Set Strategy to median for a numeric column, then train.
  4. Add a second Imputation step for a categorical column with Strategy set to most_frequent.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. A salary column is strongly right-skewed with a few very high values and some missing entries. Which strategy is usually safer than the mean?
  2. 2. What happens to the variance of a column if you fill 50% missing values with the mean?
  3. 3. In which case is it wise to keep a record of which values were missing?