The cause matters. If high earners skip an income question, filling every gap with the median hides a potentially useful pattern. If a sensor failed randomly for a few readings, deleting those rows may be harmless. Ask how the data was collected and whether missingness will occur the same way at prediction time.
Compare the strategies
Use Strategy in the visualizer to compare Drop the rows, Fill with the mean, Fill with the median, and Median + missing indicator. The plot uses a deliberately skewed example: dropping the large values changes the remaining sample, while a constant fill creates a pile-up and pulls the distribution toward the fill value.
| Method | Useful when | Main limitation |
|---|
| Drop rows | Few rows are missing and complete cases are representative | Can waste data or bias results if absence is systematic |
| Mean imputation | A numeric feature is roughly symmetric and a simple baseline is needed | Sensitive to outliers; understates variation |
| Median imputation | A numeric feature is skewed or has outliers | Creates repeated values and can weaken relationships |
| Imputation plus indicator | The fact that a value is missing may predict the outcome | Adds a feature; does not recover the missing value |
| Model-based imputation | Relationships among features are useful and complexity is justified | More assumptions, tuning, and leakage risk |
For example, if a feature has observed values 2, 3, 4, 5, 40 and two missing entries, mean imputation inserts 10.8 twice and shifts the feature upward; median imputation inserts 4 twice. Neither estimates the two unknown values exactly. An indicator adds a separate 1 for originally missing rows, letting a model learn whether absence itself is informative.
Fit preprocessing without leakage
Split the data first. Fit an imputer on training data, then apply that fitted transformation to validation and test data. In cross-validation, fit it anew inside each training fold. A scikit-learn Pipeline keeps that order. Fitting the fill value on the full dataset lets held-out rows influence training.
Some tree implementations can route missing values directly. That can be a good option when supported by the model and the missingness pattern is represented in training; compare it against an imputation baseline on held-out data.
Common mistakes with Handling Missing Values
- Filling with zero automatically: zero may be a valid measurement.
- Dropping every incomplete row across many columns: small per-column gaps can remove a large share of records.
- Assuming an indicator always helps: validate it; missingness may be noise or may change after deployment.
- Using a complex imputer without a reason: begin with a simple pipeline and measure whether added complexity improves the task.