MLCrack

    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

    TypeMeaningExample
    MCARCompletely randomSensor randomly fails
    MARDepends on observed dataIncome missingness depends on fully observed age, conditional on which it is independent of income
    MNARDepends on missing value itselfSick patients miss appointments

    Imputation Strategies

    1. Deletion: listwise or pairwise β€” only if MCAR and small proportion
    2. Simple imputation: mean/median/mode β€” quick but can bias variance
    3. KNN imputation: uses similar samples for context-aware fill
    4. MICE: iterative multivariate imputation β€” best for MAR
    5. Model-based: XGBoost handles missing values natively
    6. Indicator features: add binary is_missing columns 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.