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 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 ; 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
- 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
, 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
column vector that contains the scaled eigenvalues (divided by
) 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
normalized (with respect to
) eigenvector matrix obtained from eigendecomposition of the centered kernel matrix.
- centroids
is a
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
column vector. Element i of this vector is the mean of the ith row in the kernel matrix.
- scoreTrain
is an
matrix that contains the projection of the training data onto the first q principal components in
, 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:
In the exact method, the whole kernel matrix is constructed. The centered version can be calculated as
, where
is an
matrix with all elements equal to
. Then eigendecomposition is performed on
. The normalized version of U with respect to space
is
, 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, , from the kernel matrix and then reconstructing the complete kernel matrix by
, where
is the intersection of the selected rows and columns of
. 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
to
and reduces computation complexity from
to
.
The computational details of a low-rank approximation are as follows:
The centroids are obtained from k-means clustering; they are denoted as
.
The kernel matrix between the training data and the centroids is constructed as
.
The kernel matrix between the centroids themselves is calculated as
.
is formed, and the centered version
is calculated, where
is column means of G.
The centered kernel matrix is approximated as
, and the first c eigenvectors of
can be approximated by
.
After the eigenvector matrix U and diagonal eigenvalue matrix are obtained, the projection of the training data onto the first q principal components can be calculated as
.
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

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.