Imputing Missing Values and Removing Duplicates
This lesson explains how to handle missing values and duplicate records, the two most common data defects, which distort training and, for duplicates, inflate evaluation scores.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish the types of missing data.
- Choose an imputation method.
- Remove exact and near-duplicates before splitting data.
Types of missing data
How missing values should be handled depends on why they are missing. Values can be missing completely at random, for example when a sensor drops readings randomly; missing at random given other features, for example when younger users tend to skip an income question; or missing not at random, for example when high earners hide their income.
The mechanism matters because simple imputation, such as filling with the mean, assumes the missing values resemble the observed ones. When missingness itself carries information, as in the income example, adding a binary missing-indicator feature alongside the imputed value lets the model learn from the fact that the value was missing.
Imputation methods
Dropping rows is acceptable only when few rows are affected and they are missing at random. Mean imputation suits symmetric numeric data; median imputation suits skewed data because it is not pulled by outliers; mode imputation or an explicit "unknown" category suits categorical data. For time series, forward fill or interpolation keeps continuity. Model-based imputation, such as k-nearest neighbours or iterative regression, predicts missing values from other features and is more accurate but more complex. XGBoost can also handle missing values natively by learning a default direction at each split.
As with scaling, imputation statistics must be computed on the training set only. The following table summarises the choices.
| Data | Recommended imputation |
|---|---|
| Symmetric numeric | Mean |
| Skewed numeric | Median |
| Categorical | Mode or "unknown" category |
| Time series | Forward fill or interpolation |
| Strong relationships between features | k-NN or iterative imputation |
| Missingness is informative | Impute plus missing-indicator feature |
Fill the gaps
Deduplication
Exact duplicates are removed with distinct operations on all columns or on a business key such as transaction ID. Near-duplicates, such as the same customer entered with different spellings, need normalisation (case, whitespace, punctuation) and fuzzy matching; AWS Glue FindMatches learns matching rules from labelled examples. For text and FM training data, near-duplicate documents are found with hashing techniques such as MinHash or embedding similarity. Deduplicate before splitting data so the same example cannot land in both training and test sets.
The following diagram compares the random split with the grouped split.

Duplicates
An over-optimistic model
A product categorisation model scores 96% on its test set but 81% in production. Investigation shows that many product listings were posted several times by sellers with minor wording changes, and the random split put copies of the same listing in both training and test sets.
- Normalize titles (lowercase, remove punctuation and extra spaces).
- Group near-duplicates by embedding similarity above 0.95 and keep one listing per group.
- Split by seller or by listing group so related records stay in the same split.
The new test score of 83% is lower but honest, and it now predicts production performance. The model itself did not change; only the evaluation became trustworthy.
Which data should imputation statistics, such as the median, be computed on?
Show answer
The training data only, so no information from the validation or test data leaks into training.
Exam Tips
Remember for the exam
- Skewed numeric columns: impute with the median.
- Add a missing-indicator when missingness carries information.
- Compute imputation statistics on training data only.
- Fuzzy deduplication of records: Glue FindMatches.
- Deduplicate before splitting to avoid inflated test scores.