Data Preprocessing
1
Data Preprocessing
◼ Data Preprocessing: An Overview
◼ Data Quality
◼ Major Tasks in Data Preprocessing
◼ Data Cleaning
◼ Data Integration
◼ Data Reduction
◼ Data Transformation
◼ Summary
2
Data Quality: Why Preprocess the Data?
◼ Measures for data quality: A multidimensional view
◼ Accuracy: correct or wrong, accurate or not
◼ Completeness: not recorded, unavailable, …
◼ Consistency: some modified but some not, dangling, …
◼ Timeliness: timely update?
◼ Believability: how trustable the data are correct?
◼ Interpretability: how easily the data can be
understood?
3
Major Tasks in Data Preprocessing
◼ Data cleaning
◼ Fill in missing values, smooth noisy data, identify or remove
outliers, and resolve inconsistencies
◼ Data integration
◼ Integration of multiple databases, data cubes, or files
◼ Data reduction
◼ Dimensionality reduction
◼ Numerosity reduction
◼ Data compression
◼ Data transformation
◼ Normalization
4
Data Preprocessing
◼ Data Preprocessing: An Overview
◼ Data Quality
◼ Major Tasks in Data Preprocessing
◼ Data Cleaning
◼ Data Integration
◼ Data Reduction
◼ Data Transformation and Data Discretization
◼ Summary
5
Data Cleaning
◼ Data in the Real World Is Dirty: Lots of potentially incorrect data,
e.g., instrument faulty, human or computer error, transmission error
◼ incomplete: lacking attribute values, lacking certain attributes of
interest, or containing only aggregate data
◼ e.g., Occupation=“ ” (missing data)
◼ noisy: containing noise, errors, or outliers
◼ e.g., Salary=“−10” (an error)
◼ inconsistent: containing discrepancies in codes or names, e.g.,
◼ Age=“42”, Birthday=“03/07/2010”
◼ Was rating “1, 2, 3”, now rating “A, B, C”
◼ discrepancy between duplicate records
◼ Intentional (e.g., disguised missing data)
◼ Jan. 1 as everyone’s birthday?
6
Incomplete (Missing) Data
◼ Data is not always available
◼ E.g., many tuples have no recorded value for several
attributes, such as customer income in sales data
◼ Missing data may be due to
◼ equipment malfunction
◼ inconsistent with other recorded data and thus deleted
◼ data not entered due to misunderstanding
◼ certain data may not be considered important at the
time of entry
◼ not register history or changes of the data
◼ Missing data may need to be inferred
7
How to Handle Missing Data?
age income student credit_rating buys_computer
40 high no fair no
41 high no excellent
39 high no fair yes
38 medium no fair yes
30 yes fair yes
31 low yes excellent no
33 low yes yes
medium no fair no
30 low yes fair yes
33 medium yes fair yes
31 medium yes excellent yes
40 medium no excellent yes
31 high yes fair yes
42 medium no excellent no
8
How to Handle Missing Data?
◼ Ignore the tuple: usually done when class label is missing
(when doing classification)—not effective when the % of
missing values per attribute varies considerably
◼ Fill in the missing value manually: tedious + infeasible?
◼ Fill in it automatically with
◼ a global constant : e.g., “unknown”, a new class?!
◼ the attribute mean
◼ the attribute mean for all samples belonging to the
same class: smarter
◼ the most probable value: inference-based such as
Bayesian formula or decision tree
9
Noisy Data
◼ Noise: random error or variance in a measured variable
◼ Incorrect attribute values may be due to
◼ faulty data collection instruments
◼ data entry problems
◼ data transmission problems
◼ technology limitation
◼ inconsistency in naming convention
◼ Other data problems which require data cleaning
◼ duplicate records
◼ incomplete data
◼ inconsistent data
10
How to Handle Noisy Data?
◼ Binning
◼ first sort data and partition into (equal-frequency) bins
◼ then one can smooth by bin means, smooth by bin
median, smooth by bin boundaries, etc.
◼ Regression
◼ smooth by fitting the data into regression functions
◼ Clustering
◼ detect and remove outliers
◼ Combined computer and human inspection
◼ detect suspicious values and check by human (e.g.,
deal with possible outliers)
11
Binning Methods for Data Smoothing
❑ Sorted data for price (in dollars): 4, 8, 9, 15, 21, 21, 24, 25, 26,
28, 29, 34
* Partition into equal-frequency (equi-depth) bins:
- Bin 1: 4, 8, 9, 15
- Bin 2: 21, 21, 24, 25 40
age income student credit_rating buys_computer
high no fair no
- Bin 3: 26, 28, 29, 34 41
39
high
high
no
no
excellent
fair
no
yes
* Smoothing by bin means: 38 medium no fair yes
30 low yes fair yes
- Bin 1: 9, 9, 9, 9 31 low yes excellent no
33 low yes excellent yes
- Bin 2: 23, 23, 23, 23 64 medium no fair no
30 low yes fair yes
- Bin 3: 29, 29, 29, 29 33 medium yes fair yes
31 medium yes excellent yes
* Smoothing by bin boundaries: 40 medium no excellent yes
31 high yes fair yes
- Bin 1: 4, 4, 4, 15 42 medium no excellent no
- Bin 2: 21, 21, 25, 25
- Bin 3: 26, 26, 26, 34
12
Data Preprocessing
◼ Data Preprocessing: An Overview
◼ Data Quality
◼ Major Tasks in Data Preprocessing
◼ Data Cleaning
◼ Data Integration
◼ Data Reduction
◼ Data Transformation and Data Discretization
◼ Summary
13
Data Integration
◼ Data integration:
◼ Combines data from multiple sources into a coherent store
◼ Schema integration: e.g., [Link]-id [Link]-#
◼ Integrate metadata from different sources
◼ Entity identification problem:
◼ Identify real world entities from multiple data sources, e.g., Bill
Clinton = William Clinton
◼ Detecting and resolving data value conflicts
◼ For the same real world entity, attribute values from different
sources are different
◼ Possible reasons: different representations, different scales, e.g.,
metric vs. British units
14
Handling Redundancy in Data Integration
◼ Redundant data occur often when integration of multiple
databases
◼ Object identification: The same attribute or object
may have different names in different databases
◼ Derivable data: One attribute may be a “derived”
attribute in another table, e.g., annual revenue
◼ Redundant attributes may be able to be detected by
correlation analysis and covariance analysis
◼ Careful integration of the data from multiple sources may
help reduce/avoid redundancies and inconsistencies and
improve mining speed and quality
15
Correlation Analysis (Nominal Data)
◼ Χ2 (chi-square) test
(Observed − Expected ) 2
2 =
Expected
◼ The larger the Χ2 value, the less likely the variables are
related
◼ The cells that contribute the most to the Χ2 value are
those whose actual count is very different from the
expected count
◼ Correlation does not imply causality
◼ # of hospitals and # of car-theft in a city are correlated
◼ Both are causally linked to the third variable: population
16
Chi-Square Calculation: An Example
Play Not play Sum (row)
Football Football
Like science fiction 250(90) 200(360) 450
Not like science fiction 50(210) 1000(840) 1050
Sum(col.) 300 1200 1500
◼ Χ2 (chi-square) calculation (numbers in parenthesis are
expected counts calculated based on the data distribution
in the two categories)
(250 − 90) 2 (50 − 210) 2 (200 − 360) 2 (1000 − 840) 2
=
2
+ + + = 507.93
90 210 360 840
◼ It shows that like_science_fiction and play_football are
correlated in the group
17
Chi-Square Calculation: An Example
Play Chess Not Play Chess Sum Row
Like Science Fiction 200 (90) 250 (360) 450
Not Like Science Fiction 100 (210) 950 (840) 1050
Sum COl 300 1200 1500
(Total)
◼ Χ2 (chi-square) value = 166.134
◼ It shows that like_science_fiction and play_chess more
correlated in the group as compare to like_science_fiction
and Play football
18
Correlation Analysis (Numeric Data)
◼ Correlation coefficient (also called Pearson’s product
moment coefficient)
i =1 (ai − A)(bi − B)
n n
(ai bi ) − n A B
rA, B = = i =1
(n − 1) A B (n − 1) A B
where n is the number of tuples, A and B are the respective
means of A and B, σA and σB are the respective standard deviation
of A and B, and Σ(aibi) is the sum of the AB cross-product.
◼ If rA,B > 0, A and B are positively correlated (A’s values
increase as B’s). The higher, the stronger correlation.
◼ rA,B = 0: independent; rAB < 0: negatively correlated
19
Co-Variance: An Example
◼ Suppose two stocks A and B have the following values in one week:
(2, 5), (3, 8), (5, 10), (4, 11), (6, 14).
◼ Question: If the stocks are affected by the same industry trends, will
their prices rise or fall together?
X
n
( X i − X ) i
X = i =1
n
X = i =1
n
Data Preprocessing
◼ Data Preprocessing: An Overview
◼ Data Quality
◼ Major Tasks in Data Preprocessing
◼ Data Cleaning
◼ Data Integration
◼ Data Reduction
◼ Data Transformation and Data Discretization
◼ Summary
21
Data Reduction Strategies
◼ Data reduction: Obtain a reduced representation of the data set that
is much smaller in volume but yet produces the same (or almost the
same) analytical results
◼ Why data reduction? — A database/data warehouse may store
terabytes of data. Complex data analysis may take a very long time to
run on the complete data set.
◼ Data reduction strategies
◼ Dimensionality reduction, e.g., remove unimportant attributes
◼ Principal Components Analysis (PCA)
◼ Feature subset selection, feature creation
◼ Clustering
◼ Sampling
22
Data Reduction 1: Dimensionality Reduction
◼ Curse of dimensionality
◼ When dimensionality increases, data becomes increasingly sparse
◼ The possible combinations of subspaces will grow exponentially
◼ Dimensionality reduction
◼ Avoid the curse of dimensionality
◼ Help eliminate irrelevant features and reduce noise
◼ Reduce time and space required in data mining
◼ Allow easier visualization
◼ Dimensionality reduction techniques
◼ Principal Components Analysis (PCA)
◼ Feature subset selection, feature creation
◼ Clustering
◼ Sampling
23
Principal Component Analysis (PCA)
◼ Find a projection that captures the largest amount of variation in data
◼ The original data are projected onto a much smaller space, resulting
in dimensionality reduction. We find the eigenvectors of the
covariance matrix, and these eigenvectors define the new space
x2
x1
24
Principal Component Analysis (Steps)
◼ Given N data vectors from n-dimensions, find k ≤ n orthogonal vectors
(principal components) that can be best used to represent data
◼ Normalize input data: Each attribute falls within the same range
◼ Compute k orthonormal (unit) vectors, i.e., principal components
◼ Each input data (vector) is a linear combination of the k principal
component vectors
◼ The principal components are sorted in order of decreasing
“significance” or strength
◼ Since the components are sorted, the size of the data can be
reduced by eliminating the weak components, i.e., those with low
variance (i.e., using the strongest principal components, it is
possible to reconstruct a good approximation of the original data)
◼ Works for numeric data only
25
Attribute Subset Selection
◼ Another way to reduce dimensionality of data
◼ Redundant attributes
◼ Duplicate much or all of the information contained in
one or more other attributes
◼ E.g., purchase price of a product and the amount of
sales tax paid
◼ Irrelevant attributes
◼ Contain no information that is useful for the data
mining task at hand
◼ E.g., students' ID is often irrelevant to the task of
predicting students' GPA
26
Heuristic Search in Attribute Selection
◼ There are 2d possible attribute combinations of d attributes
◼ Typical heuristic attribute selection methods:
◼ Best single attribute under the attribute independence
assumption: choose by significance tests
◼ Best step-wise feature selection:
◼ The best single-attribute is picked first
◼ Then next best attribute condition to the first, ...
◼ Step-wise attribute elimination:
◼ Repeatedly eliminate the worst attribute
◼ Best combined attribute selection and elimination
27
Attribute Creation (Feature Generation)
◼ Create new attributes (features) that can capture the
important information in a data set more effectively than
the original ones
◼ Three general methodologies
◼ Attribute extraction
◼ Domain-specific
◼ Mapping data to new space (see: data reduction)
◼ E.g., manifold approaches
◼ Attribute construction
◼ Combining features
28
Clustering
◼ Partition data set into clusters based on similarity, and
store cluster representation (e.g., centroid and diameter)
only
◼ There are many choices of clustering definitions and
clustering algorithms
29
Sampling
◼ Sampling: obtaining a small sample s to represent the
whole data set N
◼ Allow a mining algorithm to run in complexity that is
potentially sub-linear to the size of the data
◼ Key principle: Choose a representative subset of the data
◼ Simple random sampling may have very poor
performance in the presence of skew
◼ Develop adaptive sampling methods, e.g., stratified
sampling:
◼ Note: Sampling may not reduce database I/Os (page at a
time)
30
Types of Sampling
◼ Simple random sampling
◼ There is an equal probability of selecting any particular
item
◼ Sampling random sampling without replacement
◼ Once an object is selected, it is removed from the
population
◼ Sampling random sampling with replacement
◼ A selected object is not removed from the population
◼ Cluster/ Stratified sampling:
◼ Partition the data set, and draw samples from each
partition (proportionally, i.e., approximately the same
percentage of the data)
31
Sampling: With or without Replacement
Raw Data
32
Sampling: Cluster or Stratified Sampling
Raw Data Cluster/Stratified Sample
33
Data Preprocessing
◼ Data Preprocessing: An Overview
◼ Data Quality
◼ Major Tasks in Data Preprocessing
◼ Data Cleaning
◼ Data Integration
◼ Data Reduction
◼ Data Transformation
◼ Summary
34
Data Transformation
◼ A function that maps the entire set of values of a given attribute to a new set of
replacement values s.t. each old value can be identified with one of the new values
◼ Methods
◼ Smoothing: Remove noise from data
◼ Attribute/feature construction
◼ New attributes constructed from the given ones
◼ Aggregation: Summarization
◼ (<=10000, >1000 && <=70000, >70000) -> (low, med, high)
◼ Normalization: Scaled to fall within a smaller, specified range
◼ min-max normalization
◼ z-score normalization
◼ normalization by decimal scaling
35
Normalization
◼ Min-max normalization: to [new_minA, new_maxA]
v − minA
v' = (new _ maxA − new _ minA) + new _ minA
maxA − minA
◼ Ex. Let income range $12,000 to $98,000 normalized to [0.0,
73,600 − 12,000
1.0]. Then $73,000 is mapped to 98,000 − 12,000 (1.0 − 0) + 0 = 0.716
◼ Z-score normalization (μ: mean, σ: standard deviation):
v − A
v' =
A
73, 600 − 64, 000
◼ Ex. Let μ = 64,000, σ = 16,000. Then 16, 000
= 0.6
◼ Normalization by decimal scaling
v
v' = j Where j is the smallest integer such that Max(|ν’|) < 1
10
36
Normalization
Normalize following data with in the range [-1 to 1]
37
Normalization
◼ Min-max normalization: to [new_minA, new_maxA]
v − minA
v' = (new _ maxA − new _ minA) + new _ minA
maxA − minA
38
Normalization
◼ Z-score normalization (μ: mean, σ: standard deviation):
v − A
v' =
A
X
n
( X i − X ) i
X = i =1
n
X = i =1
39
Normalization
◼ Normalization by decimal scaling
v
v' = j Where j is the smallest integer such that Max(|ν’|) < 1
10
40
Data Preprocessing
◼ Data Preprocessing: An Overview
◼ Data Quality
◼ Major Tasks in Data Preprocessing
◼ Data Cleaning
◼ Data Integration
◼ Data Reduction
◼ Data Transformation and Data Discretization
◼ Summary
41