What Is Imputation in Data Science?
Learn what imputation is, common strategies like mean, KNN and regression-based methods, missingness mechanisms, and how to avoid data leakage.
Expected Interview Answer
Imputation is the process of filling in missing values in a dataset with substituted, estimated values so the data remains usable for analysis or modeling instead of being discarded.
Simple strategies include filling missing entries with the mean, median, or mode of a column, or a constant like zero. More advanced methods use nearby values, such as forward/backward fill for time series, or predictive imputation, where a model like k-nearest neighbors or a regression estimates the missing value from other correlated features. The right method depends on why data is missing — missing completely at random, missing at random, or missing not at random — since ignoring that mechanism can silently bias the resulting model or analysis.
- Preserves sample size instead of dropping incomplete rows
- Reduces bias introduced by naive listwise deletion
- Enables models that cannot natively handle missing values
- Can leverage correlated features for more accurate estimates
- Improves the stability of downstream statistical analysis
AI Mentor Explanation
Imputation is like a scorer estimating a rained-off innings' final total using the Duckworth-Lewis method instead of leaving that match blank in the record books. Rather than deleting the whole game from the season's statistics, the scorer fills the gap with the most sensible estimate based on the overs and wickets already known, keeping the season's data usable.
How missing values are filled during imputation
Raw data
- column with NaN/missing entries
Imputation method
- mean/median/mode
- forward/backward fill
- KNN or regression-based
Result
- complete dataset with no missing values
Step-by-Step Explanation
Step 1
Detect missingness
Identify which columns have missing values and quantify how much data is affected.
Step 2
Diagnose the mechanism
Assess whether data is missing completely at random, at random, or not at random.
Step 3
Choose a method
Pick mean/median/mode for simple cases, or KNN/regression/multiple imputation for more accuracy.
Step 4
Apply and flag
Fill the missing values and optionally add an indicator column noting which values were imputed.
Step 5
Validate impact
Check that imputation hasn't distorted distributions or introduced bias into downstream models.
What Interviewer Expects
- Distinguishes simple imputation (mean/median/mode) from model-based imputation (KNN, regression)
- Understands MCAR, MAR, and MNAR missingness mechanisms
- Knows the risk of bias from naive listwise deletion
- Can mention adding a 'was_missing' indicator flag
- Understands trade-offs between simplicity and accuracy of imputation methods
Common Mistakes
- Always using mean imputation regardless of the missingness mechanism
- Ignoring that imputation can artificially shrink variance
- Imputing before splitting into train/test, causing data leakage
- Not investigating why data is missing before choosing a method
Best Answer (HR Friendly)
“Imputation is a technique for filling in missing pieces of data with reasonable estimates instead of throwing away incomplete records. It helps analysts keep more of their data usable while avoiding the bias that comes from simply deleting anything with a gap.”
Code Example
import pandas as pd
from sklearn.impute import SimpleImputer, KNNImputer
df = pd.DataFrame({"age": [25, None, 35, 40, None], "income": [50000, 60000, None, 80000, 55000]})
# Simple mean imputation
mean_imputer = SimpleImputer(strategy="mean")
simple_filled = mean_imputer.fit_transform(df)
# More accurate KNN-based imputation
knn_imputer = KNNImputer(n_neighbors=2)
knn_filled = knn_imputer.fit_transform(df)
print("Mean-imputed:\n", simple_filled)
print("KNN-imputed:\n", knn_filled)Follow-up Questions
- What is the difference between MCAR, MAR, and MNAR missingness?
- When would you prefer KNN imputation over mean imputation?
- Why is imputing before a train/test split a data leakage risk?
- What is multiple imputation and how does it differ from single imputation?
- How would you decide between dropping a column and imputing it?
MCQ Practice
1. What does imputation primarily address?
Imputation fills in missing values so incomplete records can still be used for analysis or modeling.
2. Which missingness type means the probability of a value being missing depends only on observed data?
MAR means missingness depends on other observed variables, not on the missing value itself.
3. Why can imputing data before splitting into train and test sets be problematic?
Fitting an imputer on the full dataset before splitting lets test set statistics leak into training, inflating performance estimates.
Flash Cards
What is imputation? — Filling in missing dataset values with estimated substitutes instead of discarding incomplete records.
Name three missingness mechanisms. — Missing Completely At Random (MCAR), Missing At Random (MAR), and Missing Not At Random (MNAR).
What is a simple imputation strategy? — Filling missing values with the column's mean, median, or mode.
Why add a 'was_missing' indicator column? — It preserves the signal that a value was originally missing, which can itself be predictive.