๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #393

Data Cleaning

Building directly on what Data Exploration surfaces, data cleaning is the practical process of actually fixing or removing the problems found โ€” corrupt files, duplicates, and label errors.

Common Cleaning Tasks

IssueTypical Fix
Corrupt or unreadable filesDetect and remove (or attempt to repair) programmatically before they crash training mid-run
Duplicate or near-duplicate examplesDeduplicate, especially critical across train/validation/test splits, to avoid data leakage
Mislabeled examplesRe-label manually if feasible, or remove if the error rate for a specific subset is severe and re-labeling isn't practical
Missing values (for tabular/structured features)Impute, drop, or explicitly encode "missingness" as its own signal, depending on the specific pattern of missingness

Code โ€” Detecting Corrupt Image Files

from PIL import Image
import os

def find_corrupt_images(image_dir):
    corrupt_files = []
    for filename in os.listdir(image_dir):
        filepath = os.path.join(image_dir, filename)
        try:
            img = Image.open(filepath)
            img.verify()   # checks the file is a valid, readable image without fully decoding it
        except Exception:
            corrupt_files.append(filepath)
    return corrupt_files

corrupt = find_corrupt_images("images/")
print(f"Found {len(corrupt)} corrupt files")

Code โ€” Detecting Near-Duplicates Across Splits

import hashlib

def file_hash(filepath):
    with open(filepath, 'rb') as f:
        return hashlib.md5(f.read()).hexdigest()

train_hashes = {file_hash(f) for f in train_files}
test_hashes = {file_hash(f) for f in test_files}
overlap = train_hashes & test_hashes
if overlap:
    print(f"WARNING: {len(overlap)} exact duplicate files found across train and test sets!")

This exact check directly prevents the data leakage problem flagged in Dataset Train/Val/Test Split โ€” a test example that's an exact (or near) duplicate of a training example produces an artificially inflated, misleading test performance estimate.

Common Mistakes

  • Cleaning data after splitting into train/val/test without re-checking for duplicates across the splits โ€” cleaning steps should be applied consistently, with the split-leakage check specifically done across the final splits, not just within the raw combined dataset.
  • Removing every example with any detected imperfection indiscriminately, rather than assessing severity โ€” sometimes a minor, correctable issue is better fixed than the example simply discarded, especially if data is limited.

Interview Relevance

Q: "Why is checking for duplicate examples across your train/validation/test splits specifically important, not just within the raw dataset?" If the same (or a near-identical) example appears in both the training set and the test set, the model may effectively be evaluated on data it has already memorized during training โ€” producing an artificially inflated, misleading test performance estimate that won't hold up on genuinely new, unseen data. This directly undermines the entire purpose of holding out a test set, exactly the concern flagged in Dataset Train/Val/Test Split.

Practice Question

Why might it be better to fix a minor, correctable labeling error rather than simply discarding the affected example, especially in a small dataset?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Data Cleaning โ€“ FAQs

Quick answers about learning Data Cleaning in Deep Learning.

This free note from CodingNow 2.0 explains Data Cleaning in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Data Cleaning, is 100% free with no signup required.
With focused practice, most students grasp Data Cleaning in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now