Concepts 🚀 Project 🧮 Labs
🧮 Labs / ML Advanced / Imputation — when to use mean vs median vs mode vs KNN vs MICE

Imputation — when to use mean vs median vs mode vs KNN vs MICE

Missing values are NOT a 'fill with mean' problem. Different imputation strategies for different situations. Know when to use each.

script.py v3.1
Python output click Run to execute
Click "Run" to execute the code on the left.
The output will appear here.
🧠 How it works

5 imputation strategies — pick based on the DATA: MEAN — for normal distributions (age, height). Pulled by outliers. MEDIAN — for skewed distributions (income, prices). Robust to outliers. MODE (most_frequent) — for categorical (city, color). The only option for categories. KNN — uses correlation. A 25-year-old's missing income = average of 5 similar 25-year-olds. More accurate but O(n²) — slow on large data. MICE (IterativeImputer) — trains a model per column to predict missing values from others. Most sophisticated. Best for complex patterns. Slowest. THREE missingness types you should know: MCAR (Missing Completely At Random) — safe to impute. MAR (Missing At Random) — depends on other features (KNN/MICE help). MNAR (Missing Not At Random) — depends on the missing value itself (rich hide income). Hardest.

✏️ Try these changes
  • Change income strategy from 'median' to 'mean'
    → mean is pulled up by billionaires — bad for skewed data
  • Change KNN n_neighbors from 5 to 20
    → more neighbors = smoother but less specific imputation
  • Add a constant imputer: SimpleImputer(strategy='constant', fill_value=-999)
    → marks missing as a separate value (good for tree models)
  • Add strategy='constant' fill_value='Unknown' for city
    → treats missing as its own category