Dimensionality Reduction
Kernel PCA, kPCA
2023-12-21
Both LDA and PCA are methods for linear dimensionality reduction. They reduce the dimensions of the original data to one-dimensional (LDA) or perceivable dimensions by inner product with projection vector(s). However, for many problems, linear projection methods may not be suitable to produce clusters separable or perform classification in low dimensions. For example, considering the Half Moon dataset, no matter which direction of the projection is done, there is no linear projection plane can separate the two groups. In such cases, kernel PCA (kPCA) can be used to enable clustering by expanding or transforming the original data into entirely different dimensions.
In this blog, we will explain kPCA by using the Gaussian Radial Based Function (RBF) Kernel, which is known to be the most representative one.

The kPCA starts with the idea that, for the size of nxd samples, X, the d-dimensional eigenvector, v, used in PCA can be represented by n-dimensional weights α. In summary,
Size of X = [nxd], Size of α = [nx1] which is one weight per sample
v = XTα = [dxn][nx1] = [dx1]
Substituting the relationship into the eigenvalue formula we derived for the PCA, it becomes,
\(\dfrac{1}{n}(X^TX)v=\lambda v\ \rightarrow \ \dfrac{1}{n}(X^TX)(X^T\alpha)=\lambda X^T\alpha\)
Performing the matrix multiplications by X at both sides, and introducing Kernel definition, K≡XXT, we get the eigenvalue formula for the kPCA.
\(\dfrac{1}{n}XX^TXX^T\alpha=\lambda XX^T \alpha\ \rightarrow \ \dfrac{1}{n}KK\alpha=\lambda K \alpha\ \rightarrow \ \dfrac{1}{n}K\alpha=\lambda \alpha\)
Here,
(1) K is the Kenel matrix of the size [nxn]
(2) α는 is the eigenvector of the size [nx1]
As learned in kernel SVM, various kernels K exist. Here we use the RBF Kernel between two samples defined as
\(K(x,x')=\exp (-\beta(x-x')^T(x-x'))\)
where the statistical meaning of β is 1/(2σ2), but it is the hyperparameter to be selected by a Grid search for the best results.
The plot below is one of the best results from kPCA algorithm using the Half Moon data. Here, the β=30 is selected.

The steps for the algorithm deployment are as follows:
(step 1) Set a value of β
(step 2) Build a [nxn] Kernel (vs. [dxd] Covariance matrix for PCA), then we need a Kernel centering for all the rows and columns to be zero centered by the following formula.
\(K=K-K^TI-I^TK+I^TKI\)
where I is the [nxn] matrix of ones with the value = 1/n, KTI is the column-wise average, ITK is the row-wise average, and ITKI is the grand average.
(step 3) Perform the eigenvalue analysis toward K/n matrix to find the eigenvector, α
(step 4) Find top two principal components PC1 and PC2 by projecting K into α1 and α2
PC = KTα = [nxn][nx1] = [nx1]
Steps in Excel for Kernel PCA
(step 1) Set the hyperparameter, β.
(step 2) Build the RBF Kernel of size [nxn]
Use SUMXMY2 for a calculation and INDEX function to select two samples

(step 3) Calculate the column-wise average at ROW 102 and row-wise average at COLUMN DA, and the grand average at the Cell DA102 for the Kernel centering
(step 4) Update Kernel by Kernel Centering

(step 5) Perform eigenvalue analysis by using NIPALS to find two principal eigenvectors. Please refer the PCA to use Excel's Iterative calculation.
(step 6) Calculate two principal components PC1 and PC2 by performing matrix multiplication to project K into α1 and α2
PC1 = MMULT(K,α1)
PC2 = MMULT(K,α2)
The plots below show the results for various β's at 1, 5, & 15, respectively. By adequately choosing the hyperparameter, β, the two clusters are clearly separable in the projected window.


