Data Pre-Processing-IV (Feature Extraction-PCA)_7c5a4c5da931f4f69a14c94e7e8b9062
Data Pre-Processing-IV (Feature Extraction-PCA)_7c5a4c5da931f4f69a14c94e7e8b9062
D r. JASMEET S INGH
ASSISTANT P ROFESSOR, C SED
T IET, PATIALA
Feature Extraction
Feature extraction, creates new features from a combination of original features.
For a given Feature set Fi (F1, F2, F3,……..Fn), feature extraction finds a mapping
function that maps it to new feature set Fi ’ (F1’, F2’, F3’,…….Fm’) such that Fi’=f(Fi)
and m <n.
= ∑ (𝑥 . 𝑢 − 𝑥̅ 𝑢)
Since the feature vector is centered around mean, therefore, 𝑥̅ =0 (sum of deviations from mean is
zero)
𝑚𝑎𝑥 ∑ (𝑥 . 𝑢)
Writing out all the summations grows tedious, so let’s do our algebra in matrix form. If we stack our n data
vectors into an n × k matrix, X, then the projections are given by Xu, which is an n ×1 matrix.
Therefore, 𝑢 𝑢 = 1 𝑎𝑛𝑑 𝑆𝑢 = 𝜆𝑢
Thus, u is eigen vectors corresponding to the eigen values of covariance matrix S.
Since, S is of k X k , hence there will k eigen vectors.
𝐿𝑒𝑡 𝜆 > 𝜆 > ⋯ … … … … … 𝜆 𝑏𝑒 𝑘 𝑒𝑖𝑔𝑒𝑛 𝑣𝑎𝑙𝑢𝑒𝑠 𝑜𝑓 𝑆
Mathematics behind PCA (Contd…..)
,
=0
,
= 2 𝑆 − 𝜆𝐼
,
=2 𝑆−𝜆 𝐼
Thus, the principal components that captures maximum variance of input data points are the
eigen vectors of the covariance matrix of the input feature matrix corresponding to largest eigen
values.
Principal Component Analysis- Step-wise Working
Step 1: Construction of covariance matrix named as S.
The aim of this step is to understand how the variables of the input data set are varying from the mean with respect to
each other, or in other words, to see if there is any relationship between them.
where
Alternatively, Covariance matrix, S, can be computed as
Principal Component Analysis- Step-wise Working
Step 2: Computation of eigenvalues for covariance matrix , using following equation:
𝐝𝐞𝐭(𝐒 − 𝝀𝑰)=0
The eigenvalues are simply the coefficients attached to eigenvectors, which give the amount of variance
carried in each Principal Component.
Step 3: Sort the eigenvectors in decreasing order of eigenvalues and choose k eigenvectors with the
largest eigenvalues
The eigenvectors of the Covariance matrix are actually the directions of the axes where there is the most
variance (most information) and that we call Principal Components
Step 5: Transform the data along the principal component axis.
PCA –Numerical Example
Check (mathematically) whether the following two-dimensional datapoints can
be transformed to one dimension using Principal Component Analysis.
If yes, determine the magnitude, percentage variance captured along the new
principal components and the new principal component.
Data points (x, y): {(2, 1), (3, 5), (4, 3), (5, 6), (6, 7), (7, 8)}
PCA –Numerical Example (Solution)
Step 1: Compute covariance matrix, S:
2 1
3 5
Feature matrix=X= 4 3
5 6
6 7
7 8
Mean Vector=𝜇 = 4.5 5
−2.5 −4
−1.5 0
Feature Vector centered around mean=𝑋 = −0.5 −2
0.5 1
1.5 2
2.5 3
PCA –Numerical Example (Solution)
Covariance Matrix=S=
−2.5 −4
−1.5 0
−2.5 −1.5 −0.5 0.5 1.5 2.5 −0.5 −2
=
−4 0 −2 1 2 3 0.5 1
1.5 2
2.5 3
1 17.5 22 3.5 4.4
= =
5 22 34 4.4 6.8
Alternately covariance between each pair of variables can be computed using
following equation (as shown in next slide):
PCA –Numerical Example (Solution)
Step 1 (second method)
x y 𝒙−𝒙 𝒚−𝒚 (𝒙 − 𝒙)𝟐 (𝒚 − 𝒚)𝟐 (𝒙 − 𝒙)(𝒚 − 𝒚)
2 1 -2.5 -4 6.25 16 10
3 5 -1.5 0 2.25 0 0
4 3 -0.5 -2 0.25 4 1
5 6 0.5 1 0.25 1 0.5
6 7 1.5 2 2.25 4 3
7 8 2.5 3 6.25 9 7.5
𝑣𝑎𝑟 𝑥
1 𝑣𝑎𝑟 𝑦 𝑐𝑜𝑣 𝑥, 𝑦
= (𝒙 − 𝒙)𝟐 1
𝑛−1 1
= (𝒚 − 𝒚)𝟐 = (𝒙 − 𝒙)(𝒚 − 𝒚)
=3.5 𝑛−1 𝑛−1
𝒙=4.5 𝑦=5 =6.8 =4.4
3.5 4.4
S=
4.4 6.8
PCA –Numerical Example (Solution)
Step 2: Find eigen values of covariance matrix
Characteristic Equation=|S-λI|=0
3.5 − 𝜆 4.4
=0
4.4 6.8 − 𝜆
3.5 − 𝜆 6.8 − 𝜆 − 19.36 = 0
23.8 − 6.8𝜆 − 3.5𝜆 + 𝜆 − 19.36 = 0
𝜆 − 10.3𝜆 + 4.44 = 0
10.3 ∓ 106.09 − 17.76 10.3 ∓ 9.43
𝜆= = = 9.865,0.435
2 2
Step 3: Magnitude of variance captured along first principal components= 9.865
.
Percentage of variance captured along first principal components= × 100% = 95.78%
. .
Yes, it can be transformed to one dimension because maximum variance is captured in first dimension.
PCA –Numerical Example (Solution)
Step 4: First Principal Component i.e. eigen vector for 𝜆1= 9.86
(S- 𝜆𝐈)X=O
𝟑. 𝟓 𝟒. 𝟒 𝟏 𝟎
− 𝟗. 𝟖𝟔 𝑿=𝑶
𝟒. 𝟒 𝟔. 𝟖 𝟎 𝟏
−𝟔. 𝟑𝟔 𝟒. 𝟒
𝑿=𝑶
𝟒. 𝟒 −𝟑. 𝟎𝟔
𝟏 −𝟎. 𝟔𝟗
𝑿=𝑶
𝟒. 𝟒 −𝟑. 𝟎𝟔
𝟎. 𝟒𝟕
𝑿= (𝒍𝒆𝒏𝒈𝒕𝒉 𝒏𝒐𝒓𝒎𝒂𝒍𝒊𝒛𝒆𝒅)
𝟎. 𝟔𝟖