Language Reference

KPCATRAIN call

CALL KPCATRAIN (eigenVal, eigenVec, centroids, rowMeans, scoreTrain, data, <, opt, optCluster> ) ;

This subroutine is supported by the IML procedure and the iml action.

Kernel principal component analysis (kernel PCA) is a nonlinear form of traditional PCA (Schölkopf, Smola, and Müller 1998). It uses the same basic idea as traditional PCA; that is, it seeks to project the set of data onto a low-dimensional subspace that captures the highest possible amount of variance in the data. Whereas traditional PCA performs a linear projection of the data onto a subset of the original space, kernel PCA instead uses a mapping function to embed the data onto a high-dimensional space referred to as script upper F by a linear dimensionality reduction through the "kernel trick" in that space. Different kernels correspond to different mapping functions. In this way, you can find a nonlinear subspace that contains the data. The applications of kernel PCA include nonlinear dimensionality reduction, nonlinear data classification, kernel principal component regression, image denoising, and novelty detection.

The KPCATRAIN subroutine calculates the projections of training data onto the principal components in the space script upper F; it also calculates the eigenvector matrix and eigenvalue vector of the kernel matrix. By default, the KPCATRAIN subroutine applies k-means clustering to select representative points (cluster centroids) in the input data matrix and uses a low-rank approximation method for the kernel matrix construction. The low-rank approximation method greatly reduces the complexity of the eigenvector decomposition of the kernel matrix in terms of both computation and memory, and it achieves a performance that is comparable to the exact method.

The input arguments to the KPCATRAIN subroutine are as follows:

data

is an n times m training data matrix that contains data left-parenthesis x 1 comma ellipsis comma x Subscript n Baseline right-parenthesis prime.

opt

specifies an options vector. If an element of the options vector is a missing value, a default value is used. The components of opt are as follows:

opt[1]

specifies the eigenvalue cutoff value to use to determine the rank. Note that the rank determined in this way depends on the scaling of the data. If the variables are measured in different scales, you might want to standardize the data. The cutoff value should be nonnegative and not greater than 1. By default, the value of opt[1] is 1E–8.

opt[2]

specifies the kernel type:

0

linear kernel

1

polynomial kernel

2

Gaussian kernel

By default, the value of opt[2] is 2.

opt[3]

specifies the kernel parameter, which is treated as follows:

  • For the linear kernel type, this parameter is ignored.

  • For the polynomial kernel type, this parameter is the polynomial degree, can take positive integer values, and has a default value of 3.

  • For the Gaussian kernel type, the parameter is the bandwidth value, can take real positive values, and its default value is StartRoot StartFraction 0.25 Over n left-parenthesis n minus 1 right-parenthesis EndFraction sigma-summation Underscript i comma j equals 1 Overscript n Endscripts double-vertical-bar x Subscript i Baseline minus x Subscript j Baseline double-vertical-bar squared EndRoot, where n is the number of rows in the training data matrix. This choice of default value comes from the idea in Kwok and Tsang (2004). The default parameter is not guaranteed to produce good results. It is recommended that you tune this parameter by some systematic approach, such as cross validation.

opt[4]

specifies the computation method:

0

low-rank approximation method

1

exact method

By default, the value of opt[4] is 0.

opt[5]

specifies the number of principal components to use in scoring the training data. By default, the value of opt[5] is 4.

optCluster

specifies another options vector, which contains the options that are related to k-means clustering for the low-rank approximation of the kernel matrix. If this vector contains missing values, default values are used for missing options. The components of optCluster are as follows:

optCluster[1]

specifies the k-means clustering seed initialization type:

1

fast clustering, which spreads out the initial seeds in a heuristic way.

2

k-means++ (Arthur and Vassilvitskii 2007), which spreads out the initial seeds by introducing a weighted probability distribution in seed selection.

3

random, which selects initial seeds by pure randomization.

By default, the value of optCluster[1] is 1.

optCluster[2]

specifies the number of clusters, c, in k-means clustering. This value is a positive integer larger than 1. If c is equal to or larger than n (the number of rows of input training data matrix), the exact method is used instead. By default, the value of optCluster[2] is 100.

optCluster[3]

specifies the random seed to use in centroids initialization. This value is a positive integer no larger than 2,147,483,647. By default, the value of optCluster[3] is 0, which means the internal system time is used to initialize the random number stream.

optCluster[4]

specifies the maximum number of iterations for recomputing cluster centroids. This value is a positive integer. By default, the value of optCluster[4] is 50.

optCluster[5]

specifies the convergence criterion of the k-means clustering algorithm. This value is a positive real value. By default, the value of optCluster[5] is 0.02.

optCluster[6]

specifies the number of restarts when optCluster[1] is 3 (random). The best k-means clustering result is selected among all the restarts. This value is a positive integer no larger than 2,048. By default, the value of optCluster[6] is 5.

The KPCATRAIN subroutine returns the following values:

eigenVal

is an r times 1 column vector that contains the scaled eigenvalues (divided by n minus 1) of the centered kernel matrix, where r is the rank of centered kernel matrix or centered approximated kernel matrix, and n is the number of rows in the input training data matrix.

eigenVec

is the n times r normalized (with respect to script upper F) eigenvector matrix obtained from eigendecomposition of the centered kernel matrix.

centroids

is a c times m k-means clustering centroids matrix, where c is the number of clusters and m is the number of columns in the input training data matrix.

rowMeans

is an n times 1 column vector. Element i of this vector is the mean of the ith row in the kernel matrix.

scoreTrain

is an n times q matrix that contains the projection of the training data onto the first q principal components in script upper F, where q is number of principal components.

The KPCATRAIN call implements an eigenvector decomposition on the centered kernel matrix. Currently, this call supports three types of kernel:

  • linear kernel k left-parenthesis x Subscript i Baseline comma x Subscript j Baseline right-parenthesis equals left-parenthesis x Subscript i Baseline dot x Subscript j Baseline right-parenthesis, where i comma j element-of left-parenthesis 1 comma ellipsis comma n right-parenthesis, which is equivalent to traditional PCA

  • polynomial kernel k left-parenthesis x Subscript i Baseline comma x Subscript j Baseline right-parenthesis equals left-parenthesis x Subscript i Baseline dot x Subscript j Baseline plus 1 right-parenthesis Superscript d, where d is the polynomial order

  • Gaussian kernel k left-parenthesis x Subscript i Baseline comma x Subscript j Baseline right-parenthesis equals e Superscript left-parenthesis minus double-vertical-bar x Super Subscript i Superscript minus x Super Subscript j Superscript double-vertical-bar squared slash 2 s squared right-parenthesis, where s is the Gaussian kernel parameter (bandwidth)

In the exact method, the whole kernel matrix bold upper K Subscript n times n Baseline equals left-parenthesis k left-parenthesis x Subscript i Baseline comma x Subscript j Baseline right-parenthesis right-parenthesis Subscript i j is constructed. The centered version can be calculated as bold upper K overTilde Subscript n times n Baseline equals bold upper K Subscript n times n Baseline minus bold 1 Subscript 1 slash n Baseline bold upper K Subscript n times n Baseline minus bold upper K Subscript n times n Baseline bold 1 Subscript 1 slash n Baseline plus bold 1 Subscript 1 slash n Baseline bold upper K Subscript n times n Baseline bold 1 Subscript 1 slash n, where bold 1 Subscript 1 slash n is an n times n matrix with all elements equal to 1 slash n. Then eigendecomposition is performed on bold upper K overTilde equals upper U normal upper Lamda upper U Superscript upper T. The normalized version of U with respect to space script upper F is upper U normal upper Lamda Superscript negative 1 slash 2, which is returned in eigenVec.

The low-rank approximation algorithm stems from the Nyström method (Baker 1977). The Nyström method can be treated as choosing a subset of c columns, bold upper E element-of double-struck upper R Superscript n times c, from the kernel matrix and then reconstructing the complete kernel matrix by bold upper K asymptotically-equals bold upper E bold upper W Superscript negative 1 Baseline bold upper E prime, where bold upper W is the intersection of the selected rows and columns of bold upper K. The sampling scheme is important to the performance of Nyström method. In (Zhang, Tsang, and Kwok 2008), an error analysis is conducted and the resulting error bound suggests using k-means clustering as the sampling scheme. The greater the number of clusters chosen in k-means clustering, the more accurate the reconstruction. If the number of clusters equals the number of observations, the reconstruction will be exact. Low-rank approximation reduces the memory complexity of kernel PCA from script upper O left-parenthesis n squared right-parenthesis to script upper O left-parenthesis n c right-parenthesis and reduces computation complexity from script upper O left-parenthesis n cubed right-parenthesis to script upper O left-parenthesis n c squared right-parenthesis.

The computational details of a low-rank approximation are as follows:

  1. The centroids are obtained from k-means clustering; they are denoted as z Subscript i left-parenthesis i equals 1 comma ellipsis comma c right-parenthesis.

  2. The kernel matrix between the training data and the centroids is constructed as upper E Subscript n times c Baseline equals left-parenthesis k left-parenthesis x Subscript i Baseline comma z Subscript j Baseline right-parenthesis right-parenthesis Subscript i j.

  3. The kernel matrix between the centroids themselves is calculated as bold upper W Subscript c times c Baseline equals left-parenthesis k left-parenthesis z Subscript i Baseline comma z Subscript j Baseline right-parenthesis right-parenthesis Subscript i j.

  4. bold upper P Superscript negative 1 Baseline equals upper U normal upper Lamda Superscript negative 1 slash 2 Baseline upper U Superscript upper T is formed, where upper U normal upper Lamda upper U Superscript upper T is the eigendecomposition of W.

  5. upper G equals bold upper E bold upper P Superscript negative 1 is formed, and the centered version upper G overTilde equals left-parenthesis upper G minus upper G overbar right-parenthesis is calculated, where upper G overbar is column means of G.

  6. Eigenvector decomposition on upper G overTilde Superscript upper T Baseline upper G overTilde equals upper V normal upper Lamda upper V Superscript upper T is performed.

  7. The centered kernel matrix is approximated as bold upper K overTilde equals upper G overTilde upper G overTilde Superscript upper T, and the first c eigenvectors of bold upper K overTilde Subscript n times n can be approximated by upper U almost-equals upper G overTilde upper V normal upper Lamda Superscript negative 1.

After the eigenvector matrix U and diagonal eigenvalue matrix normal upper Lamda are obtained, the projection of the training data onto the first q principal components can be calculated as upper S equals bold upper K overTilde Subscript n times n Baseline upper U normal upper Lamda Superscript negative 1 slash 2 Baseline equals upper U normal upper Lamda upper U Superscript upper T Baseline upper U normal upper Lamda Superscript negative 1 slash 2 Baseline equals upper U normal upper Lamda Superscript 1 slash 2.

Since kernel PCA is a nonlinear generalization of PCA, it can perform traditional PCA if you use a linear kernel and the exact method. For example, suppose you are interested in reproducing the principal component analysis from a call to the PRINCOMP procedure. The following call uses the Sashelp.Iris data set, which has four variables:

proc princomp data=sashelp.iris out=PCComponents cov n=3
              plots(only)=score(ncomp=2);
run;

PROC PRINCOMP creates an output data set that contains the scored data. The scores are the projection of the data onto the first three eigenvectors of the covariance matrix. The following KPCATRAIN call performs an equivalent analysis. The opt vector requests that the analysis use a linear kernel and an exact method and project the data onto three principal components. The SCORE matrix returns the principal component scores. The first two scores are plotted against each other in Figure 210.

proc iml;
use sashelp.iris;
   read all var _NUM_ into X[c=varName];
   read all var "Species" into ID;
close;

/* traditional PCA */
opt = {.,         /* eigenvalue cutoff (default) */
       0,         /* linear kernel */
       .,         /* linear kernel */
       1,         /* exact method for PCA */
       3};        /* project on 3 PCs */
optClus = j(6,1,.);   /* default values */
call KPCATrain(eigenVal, eigenVec, centroids, RowMeans, score,
               X, opt, optClus);
print eigenVal;
title "Score Plot: Kernel PCA";
title2 "Iris Data; Exact Method";
call scatter(score[,1], score[,2]) grid={x y} group=ID label={"PC1" "PC2"};

Figure 209: Eigenvalues Obtained From Kernel PCA

eigenVal
422.82417
24.267075
7.82095
2.3835093


Figure 210: Projection of Iris Data onto the First Two Principal Components

Projection of Iris Data onto the First Two Principal Components


Because eigenvectors are not unique, the columns of the SCORE matrix might differ in sign from the Prin1, Prin2, and Prin3 variables in the PCComponents data set (obtained from PROC PRINCOMP). For these data, the third column of the SCORE matrix is the negative of the Prin3 variable. For an example of kernel PCA with a nonlinear (Gaussian) kernel, see the example in the KPCASCORE function.

Last updated: May 07, 2026