sKPCA: sketched Kernel PCA

Description Usage Arguments Details Value Note Author(s) References See Also Examples

View source: R/main.R

Description

Approximate Kernel PCA with Randomized Sketches.

Usage

1
  sKPCA(trdata, tsdata=NULL,d, m=ceiling(sqrt(nrow(trdata))), Stype='ROS',  kern=rbfdot(sigma=0.5), seed=2, fast_version=0)

Arguments

trdata

a n*p matrix, the train data for KPCA.

tsdata

a n1*p matrix, the test data used for reconstrction error.

d

a positve integer, the number of principal component to be extracted.

m

the projection dimension

Stype

a character, the sketch type, include 'subGaussian', 'ROS' and 'subSampling', default as ROS.

kern

a kernel function generated by kernlab package, default as gaussian kernel.

seed

a non-negative integer, specify the random seed for generating random matrix, default as 2.

fast_version

a logical value. Here we provide two approximate way based on random sketch matrix, one faster but lower precision, other a little slower but higher precision, both much faster than original KPCA.

center

a logical value, specify sketched KPCA for original Gram matrix or centralized Gram matrix, defualt as True.

Details

This function is used to compute the principal component scores. If center=True, it perform sketched KPCA for centralized Gram matrix, otherwise for original Gram matrix.

Value

return a list with class name 'sKPCA' and including following components,

alpha

a n*d matrix, the first d principal component vectors in each column.

alpha_rotate

a n*d matrix, the first d rotated principal component vectors in each column.

prop_var

a scalar, the proportion of exaplained variance.

Note

nothing

Author(s)

Liu Wei

References

nothing.

See Also

nothing

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  library(kernlab)
  data(iris)
  test <- sample(1:150,20)
  # original KPCA from kernlab package
  kpc <- kpca(~.,data=iris[-test,-5],kernel="rbfdot",
              kpar=list(sigma=0.2),features=2)
  #print the principal component vectors
  feat <- kpc@rotated
  # Sketched Kernel PCA
  library(sKPCA)
  d <- 2
  kern <- rbfdot(sigma = 0.2)
  fkpca <- sKPCA(iris[-test,-5], d=d, seed = 8, kern=kern)
  alpha_rotate <- fkpca$alpha_rotate

  fkpca1 <- sKPCA(iris[-test,-5], d=d, seed = 8, kern=kern, fast_version = T)
  alpha_rotate1 <- fkpca1$alpha_rotate
  # original KPCA from sKPCA package
  okpca <- oKPCA(iris[-test,-5], d=d, kern=kern)
  alpha_orotate <- okpca$alpha_rotate

  # plot the principal component for diffrent methods
  par(mfrow=c(2,2))
  plot(feat,col=as.integer(iris[-test,5]),
       xlab="1st Principal Component",ylab="2nd Principal Component", main='KPCA')
  plot(alpha_orotate[,1:2],col=as.integer(iris[-test,5]),
       xlab="1st Principal Component",ylab="2nd Principal Component", main='oKPCA')

  plot(alpha_rotate[,1:2],col=as.integer(iris[-test,5]),
       xlab="1st Principal Component",ylab="2nd Principal Component", main='sKPCA1')
  plot(alpha_rotate1[,1:2],col=as.integer(iris[-test,5]),
       xlab="1st Principal Component",ylab="2nd Principal Component", main='sKPCA2')

  ## Example 2. Big data
  n <- 2000
  X <- matrix(rnorm(n*50), n, 50)
  d <- 2
  kern <- rbfdot(sigma = 0.2)
  system.time(
  fkpca <- sKPCA(X, d=d, seed = 8, kern=kern)
  )
  system.time(
    fkpca1 <- sKPCA(X, d=d, seed = 8, kern=kern, fast_version = T)
  )
  system.time(
    okpca <- oKPCA(X, d=d, kern=kern)
  )

  system.time(
    kpc <- kpca(X,kernel="rbfdot",
                kpar=list(sigma=0.2),features=2)
)

feiyoung/sKPCA documentation built on Nov. 12, 2020, 8:18 a.m.