Data Preprocessing — Cleaning, Integration & Transformation
Why real data is dirty, how to fill missing values and smooth noise, cleaning as a process, integrating sources with correlation analysis, and normalization.
▶Lecture videos
Click any timestamp — here or inside the notes — to jump the video to that moment. Your position is remembered.
Introduction to Data Preprocessing
Overview of why preprocessing is needed and the main preprocessing tasks.
00:01Why data preprocessing is needed00:48Dirty data: errors, inconsistent codes (1-2-3 vs A-B-C)01:57Why data is dirty: entry, transmission, multiple sources03:59Cleaning: inconsistencies, missing values, noise04:38Preprocessing in the knowledge discovery (KDD) process05:30Major tasks: cleaning, integration, reduction, transformation06:21Data reduction and summarisation1Why preprocessing is needed
Real-world databases and data warehouses are commonly incomplete, noisy and inconsistent. Mining dirty data gives poor results, and users don't trust results from data they believe is dirty.
Incomplete (missing values)
- Attribute of interest not available (e.g. customer info on sales records)
- Not considered important at entry time
- Misunderstanding or equipment malfunction
- Inconsistent data deleted
- History / modifications not recorded
Noisy (incorrect values)
- Faulty data collection instruments
- Human or computer errors at data entry
- Transmission errors
- Technical limits (e.g. limited buffer size)
- Inconsistent naming conventions / codes, incompatible formats (dates)
- Duplicate tuples
- In the KDD life cycle, data preprocessing comes after data understanding.
- Data preparation takes about 70–80 % of the time and effort of a knowledge discovery project.
Mining routines do have some ways of handling incomplete or noisy data, but they are not always robust. They focus mostly on avoiding overfitting. So it is useful to run the data through data cleaning routines first.
The four preprocessing techniques
| Technique | Purpose |
|---|---|
| Data cleaning | Fill missing values, smooth noise, identify/remove outliers, resolve inconsistencies |
| Data integration | Merge multiple sources into one coherent store (e.g. a data warehouse) |
| Data transformation | e.g. normalization, which improves accuracy and efficiency of distance-based algorithms |
| Data reduction | Reduce size by aggregating, removing redundant features, clustering (Week 4) |
2Data cleaning — handling missing values
Six methods, from simplest to smartest:
| # | Method | How | Weakness / note |
|---|---|---|---|
| 1 | Ignore the tuple | Drop the row. Usually done when the class label is missing | Not useful unless the tuple has several missing attributes; weak when % missing varies a lot per attribute |
| 2 | Fill in manually | A person enters each value | Time-consuming; not feasible for large data |
| 3 | Global constant | Replace all missing values with e.g. "Unknown" or −∞ | The algorithm may think "Unknown" is an interesting concept because many tuples share it, so it is not foolproof |
| 4 | Attribute mean | Use the mean of the attribute, e.g. average income Rs 56,000 | Ignores the class of the tuple |
| 5 | Attribute mean for the same class | Use the mean of tuples in the same class, e.g. average income of customers in the same credit-risk category | Smarter than 4 |
| 6 | Most probable value | Predict with regression, Bayesian inference or a decision tree | Most popular. Uses the most information from the other attributes |
Income is missing for customer C5. Choose a method and see what value is filled in.
| Cust | Credit risk | Income (Rs) |
|---|---|---|
| C1 | Low | 80,000 |
| C2 | Low | 70,000 |
| C3 | High | 30,000 |
| C4 | High | 44,000 |
| C5 | Low | ? |
"Describe methods for handling missing values" is a common question. List all six. Point out the trap in the global constant method, and say why method 6 is the most popular (it uses the most information).
3Noisy data and smoothing
Noise is a random error or variance in a measured variable. Three smoothing techniques:
1. Binning
Binning smooths a sorted value by consulting its neighbourhood (the values around it). This is local smoothing.
Sorted prices: 4, 8, 15, 21, 21, 24, 25, 28, 34
| Bin 1 | Bin 2 | Bin 3 | |
|---|---|---|---|
| Partition | 4, 8, 15 | 21, 21, 24 | 25, 28, 34 |
| By bin means | 9, 9, 9 | 22, 22, 22 | 29, 29, 29 |
| By bin boundaries | 4, 4, 15 | 21, 21, 24 | 25, 25, 34 |
- Bin 1 mean = (4 + 8 + 15) / 3 = 27 / 3 = 9
- Bin 2 mean = 66 / 3 = 22
- Bin 3 mean = 87 / 3 = 29
- Boundaries: each value goes to the closer of min/max. In bin 1, 8 is closer to 4 (gap 4) than to 15 (gap 7). In bin 3, 28 is closer to 25 (3) than to 34 (6).
- Smoothing by bin medians replaces each value with the bin's median.
- The larger the bin width, the stronger the smoothing effect.
- Bins can also be equal-width (constant interval range). There's an interactive binning lab in Week 4.
2. Regression
Fit the data to a function. Linear regression finds the best line to fit two attributes so one can predict the other. Multiple linear regression uses more than two attributes and fits a multidimensional surface.
3. Clustering
Group similar values into clusters. Values that fall outside all clusters can be considered outliers.
4Data cleaning as a process
So far we've looked at techniques. In practice cleaning is a two-step iterative process:
Step 1 — Discrepancy detection
Causes of discrepancies: - Poorly designed data-entry forms with many optional fields - Human error in data entry - Deliberate errors (respondents hiding personal information) - Data decay (outdated addresses) - Inconsistent representations and inconsistent use of codes - Instrument and system errors - Data used for purposes other than originally intended - Integration inconsistencies (the same attribute has different names in different DBs)
How to detect them: start with metadata ("data about data"). Ask: what is the domain and data type of each attribute? What are the acceptable values? Do all values fall in the expected range? Are there known dependencies?
Look out for: - Inconsistent representations: "2004/12/25" vs "25/12/2004" - Field overloading: developers squeeze new attribute definitions into unused bits of existing attributes (e.g. using the unused bit of a 32-bit field that only needs 31)
Check the data against three kinds of rules:
| Rule | Says | Example |
|---|---|---|
| Unique rule | Every value of the attribute must be different from all others | Customer ID |
| Consecutive rule | No missing values between the lowest and highest, and all values unique | Cheque numbers |
| Null rule | How blanks, "?", special characters etc. indicate null and how to handle them | Store 0 for numbers, blank for characters, turn "don't know" or "?" into blank |
Reasons for missing values (the null rule must handle these): 1. The person refuses to give the value, or it is not applicable (e.g. licence number left blank by non-drivers) 2. The data-entry person doesn't know the correct value 3. The value will be provided by a later step
Step 2 — Data transformation (to correct discrepancies)
| Tool | What it does |
|---|---|
| Data migration tools | Simple transforms, e.g. replace the string "gender" with "sex" |
| ETL tools (Extraction / Transformation / Loading) | Specify transforms through a GUI. Only a restricted set of transforms, so custom scripts are often needed too |
- It is error-prone and time-consuming.
- Some transformations introduce new discrepancies.
- Nested discrepancies only appear after others are fixed. For example, a typo "20004" in a year field only shows up once all dates are in a uniform format.
- Transforms run as batch jobs with no feedback. Failed tuples are written to a file without explanation.
- So the process lacks interactivity and needs many iterations.
Newer, interactive approaches
- Potter's Wheel is a publicly available cleaning tool that integrates discrepancy detection and transformation:
- You build transformations one step at a time on a spreadsheet-like interface, either graphically or by giving examples.
- Results show immediately, you can undo, and discrepancy checking runs in the background.
- Declarative languages: powerful extensions to SQL for specifying data-cleaning operations.
- Keep updating the metadata as you learn about the data. This speeds up cleaning of future versions.
5Data integration
Data integration combines data from multiple sources (databases, data cubes, flat files) into a coherent data store, as in data warehousing.
Issue 1 — Entity identification problem
How do we match equivalent real-world entities from different sources? Is customer_id in one DB the same attribute as cust_number in another? This is schema integration and object matching.
Solution: metadata for each attribute (name, meaning, data type, permitted range, null rules). Metadata also helps transform codes. For example, pay type may be "H"/"S" in one DB and 1/2 in another, so integration overlaps with cleaning.
Issue 2 — Redundancy
An attribute is redundant if it can be derived from other attributes (e.g. annual revenue). Inconsistent attribute naming can also create redundancy. Many redundancies can be found with correlation analysis.
Correlation coefficient (Pearson's product-moment)
For numerical attributes A and B over N tuples:
where Ā, B̄ are the means, σA, σB are the standard deviations, Σ(aibi) is the sum of the cross-products, and −1 ≤ r ≤ +1.
| r value | Meaning |
|---|---|
| r > 0 | Positively correlated: A increases as B increases. Higher r means stronger correlation, so A or B may be removed as redundant |
| r = 0 | Independent, no correlation |
| r < 0 | Negatively correlated: one increases as the other decreases |
6Data transformation
Data are transformed or consolidated into forms suitable for mining. Four kinds:
| Kind | What | Example |
|---|---|---|
| Smoothing | Remove noise: binning, regression, clustering | As above |
| Aggregation | Summary operations, used in building data cubes | Daily sales → monthly and annual totals |
| Generalization | Replace raw values with higher-level concepts via concept hierarchies | street → city → country; age → youth / middle-aged / senior |
| Normalization | Scale values into a small range such as [−1.0, 1.0] or [0.0, 1.0] | income → 0.716 |
Min-max normalization
A linear transformation. It maps value v of A from [minA, maxA] into [new_minA, new_maxA]:
Income min = $12,000, max = $98,000, map to [0.0, 1.0]. Value $73,600:
v′ = (73,600 − 12,000) / (98,000 − 12,000) × (1.0 − 0) + 0 = 61,600 / 86,000 = 0.716
Z-score (zero-mean) normalization
Uses the mean Ā and standard deviation σA:
Use it when the actual min and max are unknown, or when outliers dominate min-max normalization.
Mean income = $54,000, σ = $16,000. Value $73,600:
v′ = (73,600 − 54,000) / 16,000 = 19,600 / 16,000 = 1.225
Decimal scaling (extra, from the textbook)
Move the decimal point: v′ = v / 10j, where j is the smallest integer such that max(|v′|) < 1. For example, if values range from −986 to 917, j = 3, so −986 → −0.986.
Normalization calculations are easy marks. Write the formula, substitute, and give the answer to 3 decimals. Also be ready to say when z-score is better than min-max: when min/max are unknown or outliers are present.
7Quick revision sheet
| Concept | Remember it as |
|---|---|
| Dirty data | Incomplete · Noisy · Inconsistent |
| Prep effort | 70–80 % of KDD time |
| Missing values (6) | Ignore · Manual · Constant · Mean · Class mean · Most probable |
| Smoothing (3) | Binning · Regression · Clustering |
| Bin example | 4,8,15 | 21,21,24 | 25,28,34 → means 9/22/29 |
| Cleaning process | Discrepancy detection ⇄ transformation, iterate |
| Rules | Unique · Consecutive · Null |
| Field overloading | New attribute squeezed into unused bits |
| Tools | Migration tools, ETL tools, Potter's Wheel, declarative SQL extensions |
| Integration issues | Entity identification (use metadata) · Redundancy (use correlation) |
| Correlation r | −1…+1; >0 positive (maybe redundant), 0 independent, <0 negative |
| Transformation | Smoothing · Aggregation · Generalization · Normalization |
| Min-max | (v − min)/(max − min) × (new range) + new min → 73,600 → 0.716 |
| Z-score | (v − mean)/σ → 73,600 → 1.225; use when outliers or unknown min/max |
?Self-check quiz
12 questions on this week. Answer them all, then press Check my answers. Your best and latest scores are saved and shown on the subject page.