CLASSICAL ML Β· INTERVIEW QUESTION
How do you handle missing data in ML? Compare different imputation strategies.
π‘ intermediate Β· Feature Engineering
Hint: Consider the missing data mechanism: MCAR, MAR, MNAR.
Answer
Missing data is one of the most common real-world data challenges. The handling strategy depends on why the data is missing.
Missing Data Mechanisms
| Type | Meaning | Example |
|---|---|---|
| MCAR | Completely random | Sensor randomly fails |
| MAR | Depends on observed data | Income missingness depends on fully observed age, conditional on which it is independent of income |
| MNAR | Depends on missing value itself | Sick patients miss appointments |
Imputation Strategies
- Deletion: listwise or pairwise β only if MCAR and small proportion
- Simple imputation: mean/median/mode β quick but can bias variance
- KNN imputation: uses similar samples for context-aware fill
- MICE: iterative multivariate imputation β best for MAR
- Model-based: XGBoost handles missing values natively
- Indicator features: add binary
is_missingcolumns to preserve signal
python
from sklearn.impute import KNNImputer
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer
# KNN imputation
knn_imp = KNNImputer(n_neighbors=5)
X_filled = knn_imp.fit_transform(X)
# MICE (iterative)
mice_imp = IterativeImputer(max_iter=10, random_state=42)
X_filled = mice_imp.fit_transform(X)Best practice: understand the mechanism, try multiple methods, validate with CV. Never impute the target variable.
Related questions
- What is feature engineering? Why is it important?
- What is overfitting? How do you detect and prevent it?
- Explain the difference between bagging and boosting. When would you use each?
- Compare K-Means, DBSCAN, and hierarchical clustering. When would you use each?
- What is cluster analysis and what are its main applications?