Principal Component Analysis#
Definition#
The Principal Component Analysis (PCA) is a dimension reduction technique widely used. Given a dataset with \(n\) features, the aim is to have \(k\) feature with \(k\le n\) so as the features retain most of the variation present in all of the original variables.
Let's deep into each process. We'll use this example of dataset to explain the different steps:
f1 |
f2 |
f3 |
y |
---|---|---|---|
a1 |
b1 |
c1 |
y1 |
a2 |
b2 |
c2 |
y2 |
a3 |
b3 |
c3 |
y3 |
We assume that \(a_i, b_i, c_i\) are numeric and \(y\) is the output
Covariance matrix#
Our previous dataset could be written as matrix:
Each row represents an observation, and each column a feature.
To compute the covariance, we need the mean of each column (relative to a feature):
with \(N\) the number of observations.
Now the covariance for each feature \(f_i\) is equal to:
Following covariance rules \( \mathrm{cov}(a, b) = \mathrm{cov}(b, a) \), let’s rewrite our original matrix:
Note: The covariance values on the diagonal represent the variance of each feature.
\(\mathbf{cov(x_1, x_2)}\) > 0 if \(\mathbf{x_1}\) rises and \(\mathbf{x_2}\) rises too
\(\mathbf{cov(x_1, x_2)}\) < 0 if \(\mathbf{x_1}\) rises and \(\mathbf{x_2}\) decreases
\(\mathbf{cov(x_1, x_2)}\) = 0 if \(\mathbf{x_1}\) and \(\mathbf{x_2}\) are independent
Compute eigenvalues and eigenvectors#
Let \(\mathbf{A} \in \mathbb{R}^{n \times n}\) be a square matrix. Then \(\lambda \in \mathbb{R}\) is an eigenvalue of \( \mathbf{A} \);
\( \mathbf{x} \in \mathbb{R}^n - \{\mathbf{0}\} \) is the corresponding eigenvector of \( \mathbf{A} \) if
The eigenvalues of \( \mathbf{A} \) are roots of the characteristic equation:
with \( \mathbf{I} \) the identity matrix corresponding to \( \mathbf{A} \):
Resolving this equation (using Sarrus’s rule), we will have an equation like:
After solving this equation we got our eigenvalues \( \lambda_1, \lambda_2, \lambda_3 \). Let’s assume that:
Knowing the eigenvalues, we can then compute eigenvectors by solving the previous equation:
That lead us to three different eigenvectors (in our case because data has 3 features):
\( \mathbf{v}_1, \mathbf{v}_2, \mathbf{v}_3 \) respectively the eigenvector of \( \lambda_1, \lambda_2, \lambda_3 \).
Capture the principal components#
Now the final part is to choose the number \( k \le n \) of principal component to retain.
Let’s choose \( k = 2 \) in our case. Because of assumption in (a), we’ll retain the correspondent eigenvectors for \( \lambda_1 \) and \( \lambda_2 \): \( v1 \) and \( v2 \)
The \( n \times k \) matrix \( \mathbf{W} \) can be written as:
We use the \( 3 \times 2 \) dimensional matrix \( \mathbf{W} \) that we just computed to transform our samples onto the new subspace via the equation:
And we’ve done. ☕!
Comments
comments powered by Disqus