简体   繁体   English

在R中实现核岭回归

[英]Implementing Kernel Ridge Regression in R

I want to implement kernel ridge regression in R. My problem is that I can't figure out how to generate the kernel values and I do not know how to use them for the ridge regression. 我想在R中实现内核岭回归。我的问题是我无法弄清楚如何生成内核值,我不知道如何将它们用于岭回归。 I want to use the following kernel function: 我想使用以下内核函数:

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) }

Furthermore, I know that the formula for ridge regression is: 此外,我知道岭回归的公式是:

myridge.fit <- function(X,y,lambda) { w= solve((t(X) %% X) +(lambdadiag(dim(X)[2])), (t(X) %*% y)) return(w) }

Example training data: 示例培训数据:

           [,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

Example testing data: (I do not know if I need these at this moment) 示例测试数据:(我不知道此时是否需要这些)

      [,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

Is anyone able to help me with the first step(s). 是否有人能够帮助我完成第一步。 I have to do Ridge Regression for a RBF kernel as well as a Polynomial kernel. 我必须为一个RBF内核和一个多项式内核做岭回归。

Following is the code for polynomial kernel with degree 2, hope that helps! 以下是程度为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))

}

Solve built-in R function sometimes return singular matrix. 求解内置R函数有时会返回奇异矩阵。 You may want to write your own function to avoid that. 你可能想编写自己的函数来避免这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM