-
Notifications
You must be signed in to change notification settings - Fork 0
/
krr.r
122 lines (100 loc) · 3.76 KB
/
krr.r
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# this R script needs to be imported by calling source("krr.r") in the interpretter.
#################
# The script provides functions for plotting KRR fitted values for vaious leves
# of sigma values in the gaussian kernel for two different data sets shown below,
# plotting functions for plotting the estimated eigen functions of the underlying
# kernel operator, and executed simple CV k-folds model section methods.
# resources
# (DRR)Fast: https://cran.r-project.org/web/packages/DRR/DRR.pdf
# (CVST) reg: https://cran.r-project.org/web/packages/CVST/CVST.pdf
library(CVST)
library(DRR)
# Regular Learner with noisySinc data with minimal variance:
plotSigmaRegsinc <- function(sigvals){
#performance
mse = c()
time = c()
#data
ns = noisySinc(1000, sigma=.1, d=2)
nsTest = noisySinc(1000) #No true function
#plot
png("1-2.png")
plot(ns, xlab="x",ylab="y")
for( sig in sigvals){
# fit KRR with RBF kernel using noisySinc toy data for each sigma in kernel
krr = constructKRRLearner()
p = list(kernel="rbfdot", sigma = sig,
lambda = .1/getN(ns))
time <- c(time, as.numeric(system.time(m
<- krr$learn(ns, p))[3]))
pred <- krr$predict(m,nsTest)
mse = c(mse, sum((pred-nsTest$y)^2) / getN(nsTest))
# add lines an different colors for each sigma
lines(sort(nsTest$x), pred[order(nsTest$x)] , lty = 1)
}
dev.off()
return(list("mse" = mse, "time"= time))
}
# Regular learner with Donoho
plotSigmaRegDon <- function(sigvals){
#performance
mse = c()
time = c()
#data
ns <- noisyDonoho(1000,fun=doppler,sigma=3)
nsTest <- noisyDonoho(1000,fun=doppler)
#plot
png("3d.png")
plot(ns, xlab="x",ylab="y")
for( sig in sigvals){
# fit KRR with RBF kernel using noisySinc toy data for each sigma in kernel
krr = constructKRRLearner()
p = list(kernel="rbfdot", sigma = sig,
lambda = .1/getN(ns))
time <- c(time, as.numeric(system.time(m
<- krr$learn(ns, p))[3]))
pred <- krr$predict(m,nsTest)
mse = c(mse, sum((pred-nsTest$y)^2) / getN(nsTest))
# add lines an different colors for each sigma
lines(sort(nsTest$x), pred[order(nsTest$x)] , lty = 1)
}
dev.off()
return(list("mse" = mse, "time"= time))
}
# plot the approximations for the eigenfunctions of kernel operator
plotEigFun <- function(x,ksigma){
library(kernlab)
rbf = rbfdot(sigma = ksigma)
x = x[order(x)]
k = kernelMatrix(rbf,x)
pc = princomp(k)
eig = eigen(k)
png("evals.png")
plot(eig$values[1:50], xlab="eigenvector",ylab="eigenvalue")
dev.off()
#plot top nine e.functions
png("efunctions.png")
par(mfrow=c(3,3))
#plot(x, k %*% pc$loadings[,1], type="l", xlab = "x", ylab=expression(phi))
plot(x, sqrt(eig$values[1])/eig$values[1] * k %*% eig$vectors[,1], type="l",
xlab = "x", ylab = expression(phi))
for( i in 2:9){
#plot(x,k %*% pc$loadings[,i], lty=1, xlab="x", ylab=expression(phi))
plot(x, sqrt(eig$values[i])/eig$values[i] * k %*% eig$vectors[,i], type="l",
xlab="x", ylab = expression(phi))
dev.off()}
}
# K-folds CV with regular Donoho
cv <- function(data,sigvals, lambdavals){
krr=constructKRRLearner()
params = constructParams(kernel="rbfdot", sigma = sigvals, lambda = lambdavals)
opt = CV(data, krr, params)
png("nsfit.png")
p = list(kernel="rbfdot", sigma=opt$kernel$sigma, lambda=opt$kernel$lambda)
m = krr$learn(data, p)
plot(data, xlab="x", ylab="y", xlim = c(-3,3), ylim=c(-1.5,1.5))
pred = krr$predict(m, data)
lines(sort(data$x), pred[order(data$x)], lty=1, lwd = 5)
dev.off()
return(list("opt"=opt))
}