Lesson 3 of 5 Data Quality, Cleaning and Labeling

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.

DataRecommended imputation
Symmetric numericMean
Skewed numericMedian
CategoricalMode or "unknown" category
Time seriesForward fill or interpolation
Strong relationships between featuresk-NN or iterative imputation
Missingness is informativeImpute plus missing-indicator feature
Scenario questions

Fill the gaps

Exam skills 1.3.7

Choose an imputation method for each kind of missing data.

Log in to try this activity Interactive activities are free with an account.

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.

Why the test score was over-optimistic. Sellers posted the same listing several times with small wording changes, and a random split put copies of listing A in both training and test, so the model was tested on items it had seen. Removing near-duplicates and splitting by listing group keeps related records together and gives an honest score.
A′ and A″ are reworded copies of listing A.
True or false

Duplicates

Exam skills 1.3.7

Check why duplicates matter and how they are removed.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.