Data Quality & Cleaning
After this lesson, you will be able to:
- Spot the four biggest data problems: missing values, outliers (weird extreme values), duplicates, and inconsistencies
- Classify missingness as MCAR, MAR, or MNAR — because the type of missingness determines which imputation strategy is safe to use
- Pick the right fix for each problem — fill in blanks, remove bad rows, cap extreme values, or deduplicate — based on why the problem happened
- Build a step-by-step data cleaning process that keeps a record of every change so you can always explain or undo what you did, and measure quality with completeness, uniqueness, and validity scores
Before you start
Don't worry if this feels tedious — cleaning data is not glamorous, but it is genuinely the highest-impact skill in all of ML. Once you get the hang of it, it becomes second nature!
#Garbage In, Garbage Out — But HOW?
Data quality problems are the most common reason ML models fail in production. Not because the model architecture was wrong, not because the hyperparameters were suboptimal, but because the training data was dirty. Studies consistently show that improving data quality yields larger accuracy gains than improving model complexity.
#The Four Horsemen of Bad Data
#1. Missing Values
Missing data is the most common quality issue. Values can be missing for different reasons, and the reason dictates the solution.
Types of missingness
-
MCAR (Missing Completely at Random): The missingness has no relationship to any variable. Example: a sensor randomly drops readings due to network glitches. Safe to drop rows or impute.
-
MAR (Missing at Random): Missingness depends on observed variables but not the missing value itself. Example: younger respondents skip the "retirement savings" question. You can predict the missing values from other columns.
-
MNAR (Missing Not at Random): Missingness depends on the missing value itself. Example: high-income people refuse to report income. This is the most dangerous type — no imputation method can fully correct it.
Strategies for missing values
| Strategy | When to Use | Trade-offs |
|---|---|---|
| Drop rows | Few missing rows (<5%), MCAR | Lose data, may introduce bias if not MCAR |
| Drop columns | >50% missing, feature is not important | Lose information |
| Mean/median imputation | Numeric, MCAR, quick baseline | Reduces variance, distorts correlations |
| Mode imputation | Categorical variables | Overrepresents the most common category |
| KNN imputation | MAR, sufficient nearby data | Computationally expensive, assumes local similarity |
| Model-based imputation | MAR, complex relationships | Best accuracy, most complex, risk of data leakage |
| Indicator variable | MNAR, missingness is informative | Adds a column; lets the model learn that "missing" is meaningful |
#2. Outliers
Outliers are observations that deviate significantly from the rest. They are not automatically bad — they might be the most important data points.
Detection methods
Why the number 3 and why 1.5 times the IQR are not derived again here: the 99.7 percent area behind the z rule is derived in Descriptive Statistics & Distributions, and what the 1.5 x IQR fence flags on bell-shaped data is derived in Quantiles, Shape & Reading Real Data.
Strategies for outliers
- Investigate first: Is it a data entry error (age = 999), a measurement error (sensor malfunction), or a genuine extreme value (a billionaire in an income dataset)?
- Remove: If it is clearly an error. Document why.
- Cap/Winsorize: Replace extreme values with a percentile threshold (e.g., cap at 99th percentile). Preserves the observation but limits its influence.
Try it! Open the Python REPL and type these lines yourself. Create a small array with an outlier:import numpy as np; data = np.array([10, 12, 11, 13, 999]); print(np.mean(data), np.median(data))— notice how the mean is wrecked by the outlier but the median barely moves!
- Log transform: Compresses the scale, reducing the influence of large values. Works well for right-skewed data.
- Keep as-is: If outliers are genuine and informative (fraud detection, rare disease diagnosis), removing them removes the signal.
You are building a credit card fraud detection model. Your dataset has 0.1% fraudulent transactions, and they all look like 'outliers' compared to normal transactions. Should you remove them?
Z-score and IQR are the entry points. For multivariate outliers (Mahalanobis), density-based detection (LOF, DBSCAN), and tree-based isolation (Isolation Forest), see the dedicated Outlier DetectionOutlier DetectionFinding points that do not belong, then deciding to drop, cap, transform or keep them.Learn more lesson next.
#3. Duplicates
Duplicate records inflate the apparent size of your dataset and bias your model toward the duplicated observations.
Types of duplicates
- Exact duplicates: Identical across all columns. Easy to detect with hashing or exact comparison.
- Fuzzy duplicates: Nearly identical but with minor differences (typos, formatting). "John Smith" vs "john smith" vs "J. Smith". Require string similarity metrics (Levenshtein distance, Jaro-Winkler).
- Semantic duplicates: Different representations of the same entity. "NYC" vs "New York City" vs "New York, NY".
#4. Inconsistencies
Inconsistencies are conflicting or invalid values:
- Format inconsistencies: Dates as "2024-01-15" vs "01/15/2024" vs "Jan 15, 2024"
- Unit inconsistencies: Mixing meters and feet, Celsius and Fahrenheit
- Encoding inconsistencies: "Male"/"Female" vs "M"/"F" vs 0/1
- Logical inconsistencies: End date before start date, age negative, graduation year before birth year
#A Systematic Cleaning Pipeline
Raw Data
|
v
1. Profile: Compute statistics, null rates, cardinality, distributions
|
v
2. Deduplicate: Remove exact duplicates, flag fuzzy matches for review
|
v
3. Validate types: Cast columns to correct types, flag failures
|
v
4. Fix inconsistencies: Standardize formats, units, encodings
|
v
5. Handle outliers: Investigate, then cap/remove/keep per column
|
v
6. Handle missing: Choose strategy per column based on missingness type
|
v
7. Validate constraints: Range checks, referential integrity, business rules
|
v
Clean Data + Cleaning Report (what was changed and why)
#Try It Yourself
Tests: Verify duplicates are removed, invalid ages are caught, salary outliers are capped, and missing values are imputed. Check that the final dataset has no nulls.
#Deep Dive: Imputation Strategies
The table above says mean imputation reduces variance. How much it reduces it can be worked out exactly.
Tests: Verify that drop strategy reduces record count. Verify mean and median strategies preserve all records. Verify imputed values are reasonable.
#Measuring Data Quality
Data quality is not binary — it exists on a spectrum. Useful metrics:
Track these metrics over time. A sudden drop in completeness or validity signals an upstream data issue that will degrade your model.
#The Cost of Not Cleaning
How much does bad data cost?
- Wasted compute: Training on dirty data wastes GPU hours on learning noise.
- Wrong predictions: Models learn the noise, not the signal, leading to worse accuracy.
- Debugging time: When a model fails in production, dirty data is the last thing people check but the most common cause.
- Trust erosion: Stakeholders who see wrong predictions lose trust in ML, making future projects harder to fund.
A widely quoted 2016 IBM estimate put the cost of bad data to the US economy at $3.1 trillion per year; treat it as a dated headline figure, not a measurement (as of 2016, verify before you cite it). For ML specifically, practitioners often say data cleaning takes a large share of project time, and surveys vary widely, so measure your own team. That time could go into modeling if data quality were higher upstream.
#Key Takeaways
- The four horsemen of bad data are missing values, outliers, duplicates, and inconsistencies — Each requires a different strategy, and the root cause (not just the symptom) determines the correct fix
- Missingness type dictates the imputation strategy. MCAR is safe to drop or impute simply, MAR can be predicted from other columns, and MNAR (where missingness depends on the missing value itself) is the most dangerous and hardest to correct
- Outliers are not automatically bad. In fraud detection or anomaly detection, the outliers ARE the signal; always investigate whether an extreme value is an error, a measurement artifact, or genuine data before deciding to remove, cap, or keep it
- Clean training data the same way as production data. Every cleaning step must be encapsulated in a reproducible function applied identically during training and inference, and imputation statistics must be computed on training data only to prevent leakage
- Data quality improvement yields larger gains than model complexity. Improving data quality consistently produces bigger accuracy improvements than switching to a fancier model architecture
#Quick Check
You discover that 30% of 'income' values are missing, and the missing rate is much higher for respondents under 25. What type of missingness is this?
Data observability platforms (Monte Carlo, Soda, Great Expectations, Pandera) turn the rules in this lesson into automated checks that fail loudly at ingest time, before the bad batch corrupts downstream state.