Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #404

Missing Value Imputation

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

StrategyBest ForFormula / Logic
Mean imputationNumeric, roughly symmetric distributionsFill with \(\bar{x} = \frac{1}{n}\sum x_i\)
Median imputationNumeric, skewed distributions or data with outliersFill with the middle value — robust to extreme values
Mode imputationCategorical dataFill with the most frequent category
KNN imputationWhen features correlate — use similar rows to estimateFill using the average of the k nearest complete rows
Constant / "Unknown"When missingness itself might be meaningfulFill 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.

Related ML Notes

Want to go beyond the notes?

Join CodingNow's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →