← All algorithms

Dimensionality Reduction

Principal Component Analysis, PCA

2023-12-21

​Principal component analysis is one of the dimensionality reduction methods in unsupervised learning. As we have shown in LDA, it also linearly maps the data (i.e. inner product with a projection vector). In LDA, the inner product of vectors is used to multiply a corresponding weight to each feature and transform them into a single variable to facilitate classification. However, PCA uses the eigenvectors of the covariance matrix as weights.

The picture below shows some scattered data in the source parameters of X1 and X2 domain. As the analysist perspective, we would like to have the data scattered as wide as possible for the given source parameter variation so that we have a better chance to discern the effect of the parameter.

Mathematical description of PCA is to find the projection plane to maximize the scatter. Let's consider the standardized data X and a projection unit vector, v, which has the same feature space with X. Since X is standardize, X = \(N(0, \sigma^2)\) and v is the unit vector, |v|=1.

​

​

The variance, V, of the projected results XTv becomes

\(V=\dfrac{1}{n}\sum _{\ }^{\ }(X^Tv)^2=\dfrac{1}{n}(X^Tv)^T(X^Tv)=\dfrac{1}{n}v^TXX^Tv\)

Here, note that since the mean of X is zero, the mean of XTv is also zero. Our problem is to maximize the above variance with respect to v with the constraint of vTv = 1.

The objective function:

\(\max_v \dfrac{1}{n}v^TXX^Tv\)

The constraint:

\(v^Tv-1 = 0\)

We can solve the optimization problem by Lagrange multiplier method with a Lagrange multiplier, \(\lambda\).

\(\max_v \dfrac{1}{n} v^TXX^Tv + \lambda(1-v^Tv)\)

The solution to satisfy ∂L/∂v = 0 is

\(\partial L/\partial v = 0 = \dfrac{1}{n}XX^Tv - \lambda v\)

\(\dfrac{1}{n}XX^Tv = \lambda v\)

This is nothing but the eigenvalue problem. Here, 1/n (XXT) is the Covariant matrix of the standardized X and v is the eigen vector.

In general, QR Factorization is suitable to perform eigenvalue analysis for high-dimensional data, but it is not applicable to Excel framework. Instead, we will use an iterative method so-called NIPALS (nonlinear iterative partial least square) and implement it in Excel worksheet by using an Excel's optional feature for iterative calculation.

The way implementing NIPALS is as follows:

Note that as assumed initially, the target matrix X must be zero mean centered by standardization.

Let's assume that u is an arbitrary random projection vector at the beginning,

(step 1) \(v = X^Tu\)

(step 2) \(v' = \dfrac {v}{\vert v \vert}\)

(step 3) \(u = X^Tv'\)

(step 4) repeat steps (1,2,3) until u got converged.

The final u becomes eigenvector, and inner product (v,u) = vTu becomes eigenvalue.

In order to find the next eigenvector, we need to take the regenerated matrix by the eigenvector u and v by uvT from X as below. Note that uTv results in constant [1x13][13x1] = [1x1] while uvT creates [13x1][1x13] = [13x13] matrix.

\(X=X-uv^T\)

After finding the two principal eigenvectors u1 and u2, the first and second principal component PC1 and PC2 can be obtained from performing the dot products as follows:

\(\text{PC1}=X\cdot u_1=X^Tu_1\)

\(\text{PC2}=X\cdot u_2=X^Tu_2\)

The plot below shows the results of PCA using the Wine dataset which has 13 features and 3 classes. The projected data in PC1 and PC2 axis shows the decent clustering.

​

The flow of the algorithm is recaptured in the steps below. We will show step by step procedures in Excel.

(step 1) Standardize the sample data to their mean

(step 2) Calculate the covariance matrix

(step 3) Use NIPALS to obtain the first two principal eigenvectors.

(step 4) Project the standardized data onto each eigenvector to obtain the principal components.

(step 5) Apply the clustering algorithm as needed.

​The sequence in Excel, including repetitive calculations for explicit interpretation, is as follows. The data used for the explanation is the Wine data, with a total of 178 samples (n=178) and 13 features (d=13). Although the 178 instances are grouped into 3 categories of wines, for the purpose of the explanation, these categories have been disregarded and assumed for unsupervised learning.

​

(step 1) Standardize the data by mean and sigma. Use Excel function STANDARDIZE.

Use Cell Function. (1) Select the destination cells (O2:AA179) first and (2)=STANDARDIZE(B$2:B$179,AVERAGE(B$2:B$179),STDEV(B$2:B$179), (3) Cntl+Shft+Entr

​

(step 2) Calculate Covariance Matrix, whose size is [dxd].

COVAR(a,b) returns the \(\sum_{i=1}^{n} \dfrac {a_ib_i}{n}\).

We use the INDEX to return a target column data from the whole X for the COVAR calculation. SUMPRODUCT(A,B)/COUNT(A) will yield same results.

​(step 3) Perform eigenvalue analysis on the above 13x13 covariance matrix using NIPALS. In order to do that, copy 13x13 covariance matrix into a new worksheet.

(step 3-1) Use the first column of covariance array as the initial u vector.

u = X(:,1), Column P = Column B

(step 3-2) Perform inner product, v = XTu

=MMULT(X,u)

​

(step 3-3) Normalize v such that |v|=1.

nv = v/SQRT(SUMSQ(v))

(step 3-4) Revise u by XTnv.

u = MMULT(X, nv)

Repeat the steps (3-2, 3-3, & 3-4) until uTnv get converged.

​This "Repeating" is doable by Copy and Paste into new columns, but Excel provides optional feature called "iterative calculation". Using this option saves time and reduces mistakes by automating the calculation.

Follow the exact instruction below.

First, steps through until the 2nd round "nv" calculation as below. Next, when calculate next u = MMULT(X,nv), go back to Column S and let the previous u at Column S refer the 2nd "nv" vector for MMULT(X,nv) calculation.

​

which will result in the following circulation reference error as below.

​You can ignore the error now. It will disappear once we turn on the iterative calculation option.​ Go to Excel's File->Options->Formulas to invoke the option window. Click the Enable button and set the Maximum Iterations = 20 which is sufficient and click OK button to execute it.

​

Resulting u vector at Column S is the eigenvector, and SUMPRODUCT(u,nv) is the eigenvalue associated. In order to find next eigenvector, revise the covariance matrix as below and repeat the steps.

\(X=X-v \cdot u^T\)

​X = X-MMULT(S2:S14,TRANSPOSE(U2:U14))

The two principal eigenvalues and eigenvectors are as listed below.​

​

(step 4) Find the principal components by projecting the standardized sample data into the two eigenvectors (u1 and u2) by matrix multiplication. The results are shown in the earlier plot.

PC1 = MMULT(X, u1)

PC2 = MMULT(X, u2)