How to configure kernel function in ksvm kernlab package?

I have latitudes and longitudes, so I need to redefine the RBF core to exp (-1/2 || sophere spance || ^ 2), which means I need to rewrite the kernel function myself. I write my kernel as follows:

round.kernel <- function(x,y){ sigma <- 1 #R <- 6371 R <- 1 a <- (sin( (x[1]-y[1])/2 ))^2+cos(x[1])*cos(y[1])*(sin((x[2]-y[2])/2))^2 c <- 2*atan2(sqrt(a),sqrt(1-a)) d <- R*c res <- exp(-d^2/(2*sigma)) return (res) } class(round.kernel) <- "kernel" 

I tested the function, the kernel must be correct. But with the following tutorial, I get an error message:

 fit <- ksvm(y=train[,2],x=train[,3:4],kernel=round.kernel,type='eps-svr') Error in .local(x, ...) : List interface supports only the stringdot kernel. 

The more complicated thing, I tried the sample code in the ksvm document:

 k <- function(x,y) {(sum(x*y) +1)*exp(-0.001*sum((xy)^2))} class(k) <- "kernel" 

But I get the same error.

Does anyone know how to correctly determine the kernel function?

+6
source share
1 answer

My problem is solved as follows: the kernel codes are correct, I must directly define the function (x, y) and declare its class as "core". The problem is even in the document that ksvm supports x, y styles, they actually do not work. Changing it in the style of these formulas can finally make everything work:

 fit <- ksvm(Freq~lat+lon,data=train[,2:4],kernel=roundrbf,type='eps-svr') 

In addition, I also read the source code of rbfdot and other kernels defined in the kernlab core itself. Note that their code style is as follows:

 function(params){ val <- function(x,y){ # True kernel defined here } return (new ("kernel_name",.Data=val,kpar=list(params))) } 

But seriously, I tried, and performing kernel functions in this style will not work. The work path is similar to this style:

 k <- function(x,y){ #calculate the result } class(k) <- "kernel" 
+6
source

Source: https://habr.com/ru/post/947066/


All Articles