我想在R中实现内核岭回归。我的问题是我无法弄清楚如何生成内核值,我不知道如何将它们用于岭回归。我想使用以下内核函数:
kernel.eval <- function(x1,x2,ker) { k=0 if (kertype == 'RBF') {
# RBF kernel
k=exp(-sum((x1-x2)*(x1-x2)/(2*kerparam^2))) } else { # polynomial kernel k=(1+sum(x1*x2))^ker$param } return(k) }
此外,我知道岭回归的公式是:
myridge.fit <- function(X,y,lambda) { w= solve((t(X) %% X) +(lambdadiag(dim(X)[2])), (t(X) %*% y)) return(w) }
示例培训数据:
[,1] [,2]
[1,] -1.3981847 -1.3358413
[2,] 0.2698321 1.0661275
[3,] 0.3429286 0.8805642
[4,] 0.5210577 1.1228635
[5,] 1.5755659 0.2230754
[6,] -1.2167197 -0.6700215
示例测试数据:(我不知道此时是否需要这些)
[,1] [,2]
[1,] -2.05 -2.050
[2,] -2.05 -2.009
[3,] -2.05 -1.968
[4,] -2.05 -1.927
[5,] -2.05 -1.886
[6,] -2.05 -1.845
是否有人能够帮助我完成第一步。我必须为一个RBF内核和一个多项式内核做岭恢复。
答案 0 :(得分:4)
以下是度为2的多项式内核的代码,希望有所帮助!
poly.kernel <- function(v1, v2=v1, p=2) {
((as.matrix(v1) %*% t(v2))+1)^p
}
KernelRidgeReg <- function(TrainObjects,TrainLabels,TestObjects,lambda){
X <- TrainObjects
y <- TrainLabels
kernel <- poly.kernel(X)
design.mat <- cbind(1, kernel)
I <- rbind(0, cbind(0, kernel))
M <- crossprod(design.mat) + lambda*I
#crossprod is just x times traspose of x, just looks neater in my openion
M.inv <- solve(M)
#inverse of M
k <- as.matrix(diag(poly.kernel(cbind(TrainObjects,TrainLabels))))
#Removing diag still gives the same MSE, but will output a vector of prediction.
Labels <- rbind(0,as.matrix(TrainLabels))
y.hat <- t(Labels) %*% M.inv %*% rbind(0,k)
y.true <- Y.test
MSE <-mean((y.hat - y.true)^2)
return(list(MSE=MSE,y.hat=y.hat))
}
求解内置R函数有时会返回奇异矩阵。你可能想编写自己的函数来避免这种情况。