Data Cleaning

What Is Data Cleaning?

Data cleaning is the process of identifying and fixing problems in a dataset before using it to train a machine learning model.

Raw Data May Contain
  • Missing values
  • Duplicate records
  • Incorrect values
  • Inconsistent formatting
  • Outliers
  • Mislabeled examples

If these problems are not fixed, the model may learn incorrect patterns.

Why Data Cleaning Matters

Clean Data

Clean Data → Better Learning → More Reliable Predictions

Messy Data

Messy Data → Confused Learning → Incorrect Predictions

Example

A house price dataset with incorrect prices and missing locations will produce unreliable predictions.

Duplicate Records

Duplicate records are rows that appear more than once in the dataset. They can cause the model to overfit to repeated examples.

Example

Customer ID 101 appears 3 times with the same name, email, and purchase history.

Problem — The model overweights this customer's patterns

Accidental Duplicate

Same record entered twice by mistake. Should be removed.

Not a Duplicate

Two different people with the same name at the same address. Should be kept.

Missing Values

Missing values occur when some data points are incomplete. They can cause errors during training if not handled properly.

Example

Name: Alice

Age: (missing)

City: Toronto

Problem — Missing age prevents the model from using that feature for this record

House Price Dataset

If location is missing for many records, the model cannot learn how location affects price.

Handling Missing Values

01

Remove the row — Simple, but reduces dataset size

02

Fill with mean — Good when there are no major outliers

03

Fill with median — Better when outliers are present

04

Fill with mode — Most common value, good for categories

05

Predict the value — Use another model to estimate the missing value

Remove Rows — Trade-Offs

Advantages
  • Simple to implement
  • Avoids making assumptions
  • Produces a clean complete dataset
Disadvantages
  • Reduces dataset size
  • May lose important examples
  • Can introduce bias if rows from one group are removed more often

Mean vs Median for Filling

Income Column — 15 Missing Values

Mean — $52,000 (affected by high earners)

Median — $48,000 (not affected by extremes)

If one person earns $2,000,000, the mean becomes much higher than typical.

Recommendation — Use median when the data has significant outliers

Handling Missing Categories

Example

Problem — Color column has 40 rows with no value

Option 1 — Replace with most common color

Option 2 — Create a new "Unknown" category

Creating an "Unknown" category is often better because it preserves the fact that color was missing.

Inconsistent Formatting

Inconsistent formatting occurs when the same type of information is stored in different ways.

Country Field — All Mean the Same Country
  • United States
  • US
  • U.S.A
  • usa

Fix — Standardize all values to "United States"

Common Formatting Issues

Capitalization — spam, Spam, SPAM → spam

Extra spaces — "Canada " → "Canada"

Different units — 5 feet and 152 cm in same column

Different date formats — 2026-06-09 and 06/09/2026

Incorrect Values

Examples of Incorrect Values
  • A person's age listed as 250
  • A house price listed as negative
  • A temperature of 500°C for a room
  • A test score of 150 out of 100

Outliers — Error vs Signal

Not every outlier is a mistake. Some extreme values are genuine and should be kept.

Error Outlier

Age listed as 999

This is impossible and should be removed or corrected.

Signal Outlier

A CEO's salary that is genuinely much higher than others

This is real data and may be important to keep.

House Price Example

Prices: $200,000 / $220,000 / $215,000 / $3,000,000

The $3,000,000 house may be a luxury home (keep) or a data entry error (remove).

Always investigate before deciding

Incorrect Labels

Example

An image of a cat is labeled as "dog."

Problem — This teaches the model incorrect information

Label problems can happen from rushed labeling, unclear guidelines, or human disagreement.

Bias in Cleaning

Example of Cleaning Bias

If you remove all records where income is missing, and lower-income people had more missing data, the remaining dataset will be biased toward higher-income examples.

Always check whether data removal creates imbalance

The Data Cleaning Process

01

Explore the data — Check for obvious problems and understand the dataset

02

Find duplicates — Identify and remove exact duplicate records

03

Handle missing values — Fill, estimate, or remove missing data

04

Fix incorrect values — Correct or remove values that are clearly wrong

05

Standardize formatting — Apply consistent formats across the dataset

06

Handle outliers — Investigate and decide whether to keep or remove

07

Fix incorrect labels — Review and correct mislabeled examples

08

Validate — Re-check the dataset before training begins

Real-World Example: Email Spam Dataset

Problems Found
  • 2,000 duplicate emails
  • 300 emails with missing subject lines
  • Labels inconsistent: "spam", "Spam", "SPAM"
Steps Taken
  • Remove 2,000 duplicate emails
  • Fill missing subjects with "no subject"
  • Standardize all labels to lowercase

Real-World Example: House Price Dataset

Problems Found
  • 500 houses missing location
  • 10 houses with negative price
  • 3 houses listed with 0 bedrooms
Steps Taken
  • Fill missing locations with most common city in the region
  • Remove 10 houses with negative prices
  • Investigate 0-bedroom listings before deciding whether to keep or remove

Data Cleaning vs Data Preprocessing

Data CleaningData Preprocessing
Fixes problems in the dataTransforms data for model use
Removes duplicatesScales numerical features
Handles missing valuesEncodes categorical features
Corrects incorrect valuesTokenizes text
Standardizes formatsResizes images
Fixes labelsSplits into train/val/test

Common Cleaning Tools

Tools

Pandas (Python) — Remove duplicates, fill values, filter rows

NumPy — Detect and handle outliers

OpenRefine — Visual data cleaning for non-programmers

Excel / Google Sheets — For small datasets

Common Mistakes

Mistakes to Avoid
  • Removing too many rows and shrinking the dataset too much
  • Always filling missing values with mean without checking for outliers
  • Ignoring inconsistent formatting
  • Not checking whether cleaning introduces bias
  • Removing outliers that are actually valid extreme values

Data Leakage in Cleaning

Mistake Approach

Calculate the mean of the entire dataset including test data, then use it to fill missing values.

Test data was used to inform training — this is data leakage.

Better Approach

Calculate the mean using only the training data. Apply that same value to fill missing data in both training and test sets.

Best Practices

Best Practices
  • Always explore data before cleaning
  • Document every change you make
  • Validate the data after cleaning
  • Avoid introducing bias when removing records
  • Be careful about how missing values near decision boundaries are handled

Key Takeaways

Summary
  • Data cleaning fixes problems in raw data before training
  • Common problems include duplicates, missing values, incorrect values, and inconsistent formatting
  • Cleaning decisions should be thoughtful to avoid bias or data loss
  • Data leakage can occur during cleaning if test data is used to inform the process
  • Clean data leads to more reliable machine learning models

Need Help?

Ask the AI if you need help understanding data cleaning steps, handling missing values, fixing inconsistent formats, or identifying outliers.