kPCA {BKPC} | R Documentation |
Kernel PCA is a nonlinear generalization of principal component analysis.
## Default S3 method: kPCA(x, theta = NULL, ...) ## S3 method for class 'kern' kPCA(x, ...) ## S3 method for class 'kernelMatrix' kPCA(x, ...)
x |
either: a data matrix, a kernel matrix of class |
theta |
the inverse kernel bandwidth parameter for the Gaussian kernel. |
... |
Currently not used. |
The data can be passed to the kPCA
function in a matrix
and the Gaussian kernel (via the gaussKern
function) is used to map the data to the high-dimensional feature space where the principal components are computed. The bandwidth parameter theta
can be supplied to the gaussKern
function, else a default value is used. Alternatively, the Gaussian kernel matrix of class "kern"
can be supplied to the kPCA
function directly. In addition, kPCA
also supports input in the form of a kernel matrix of class "kernelMatrix"
(in package kernlab) thus allowing for other kernel functions.
An object of class "kPCA"
including:
KPCs |
The original data projected on the principal components. |
Es |
the corresponding eigenvalues. |
Vecs |
a matrix containing principal component vectors (column wise). |
K |
a kernel matrix of class |
theta |
if Gaussian kernel was calculated, this is the bandwidth parameter used in its calculation. |
x |
if supplied, the original data matrix. |
The predict function can be used to embed new data on the new space
K. Domijan
Schoelkopf B., A. Smola, K.-R. Mueller : Nonlinear component analysis as a kernel eigenvalue problem. Neural Computation 10, 1299-1319.
gaussKern
kpca
(in package kernlab)
kernelMatrix
(in package kernlab)
data(iris) testset <- sample(1:150,20) train <- as.matrix(iris[-testset,-5]) test <- as.matrix(iris[testset,-5]) # make training set kernel gk <- gaussKern(train) Ktrain <- gk$K image(Ktrain) # make testing set kernel gk2 <- gaussKern(train, test, gk$theta) Ktest <- gk2$K # make training set kernel using kernelMatrix from library kernlab. library(kernlab) kfunc <- laplacedot(sigma = 1) Ktrain2 <- kernelMatrix(kfunc, train) image(Ktrain2) # make testing set kernel using kernelMatrix {kernlab} Ktest2 <- kernelMatrix(kfunc, test, train) # Do KPCA: kpcData <- kPCA(train) kpcKern <- kPCA(Ktrain) kpcKern2 <- kPCA(Ktrain2) # plot the data projection on the principal components pairs(kpcData$KPCs[ , 1 : 3], col = iris[-testset, 5]) # proportion of variance explained by each PC plot(kpcData$Es/sum(kpcData$Es), xlab = "PC", ylab = "Proportion of variance") # embed data from the testing set on the new space: KPCpred1 <- predict(kpcData, test) KPCpred2 <- predict(kpcKern, Ktest) KPCpred3 <- predict(kpcKern2, Ktest2) #plot the test data projection on the principal components pairs(KPCpred3[ , 1 : 3], col = iris[testset, 5])