Imputation means filling in missing values with a reasonable estimate instead of dropping the row entirely — trading a small amount of introduced bias for keeping more of your data.
Common Imputation Strategies
| Strategy | Best For | Formula / Logic |
|---|---|---|
| Mean imputation | Numeric, roughly symmetric distributions | Fill with \(\bar{x} = \frac{1}{n}\sum x_i\) |
| Median imputation | Numeric, skewed distributions or data with outliers | Fill with the middle value — robust to extreme values |
| Mode imputation | Categorical data | Fill with the most frequent category |
| KNN imputation | When features correlate — use similar rows to estimate | Fill using the average of the k nearest complete rows |
| Constant / "Unknown" | When missingness itself might be meaningful | Fill with a fixed placeholder category |
Why Median Beats Mean on Skewed Data
For incomes \([30000, 32000, 35000, 40000, 500000]\): the mean is \(\bar{x} = 127400\) — dragged far up by one outlier — while the median is \(35000\), representative of a typical value. Imputing missing incomes with the mean here would inject unrealistic values into most rows.
Python Implementation
import pandas as pd
from sklearn.impute import SimpleImputer, KNNImputer
df = pd.read_csv("customers.csv")
# Median imputation for a numeric column
median_imputer = SimpleImputer(strategy="median")
df["income"] = median_imputer.fit_transform(df[["income"]])
# Mode imputation for a categorical column
mode_imputer = SimpleImputer(strategy="most_frequent")
df["city"] = mode_imputer.fit_transform(df[["city"]]).ravel()
# KNN imputation — uses similar rows (by other features) to estimate the missing value
knn_imputer = KNNImputer(n_neighbors=5)
df[["income", "age"]] = knn_imputer.fit_transform(df[["income", "age"]])
Critical rule: fit the imputer only on the training set (fit_transform), then apply the same learned statistic to the test set (transform only) — otherwise information from the test set leaks into training. See Data Leakage.
Advantages
- Preserves sample size instead of discarding rows with any missing field
- Simple imputers are fast, deterministic, and easy to explain
Limitations
- Reduces the natural variance in the data — filling many gaps with the same mean/median artificially clusters values
- Can introduce bias if the missingness isn't random (see MNAR)
- KNN imputation is more accurate but slower on large datasets
Common Mistakes
- Using mean imputation on a skewed distribution without checking for outliers first.
- Computing the imputation statistic (mean/median/mode) on the full dataset instead of the training set only.
Interview Relevance
Q: "When would you choose median imputation over mean imputation?" When the feature is skewed or contains outliers — the mean is pulled toward extreme values while the median stays representative of the typical case.
Practice Question
A numeric column has values \([10, 12, 11, 13, 90]\) with one missing entry. Compute both the mean and median of the observed values, and explain which you'd use to impute.