如何在kernlab的ksvm中使用两个以上的参数编写新内核?

时间:2018-09-11 15:53:43

标签: r svm

我想编写一个具有新距离函数的新内核。但是我找不到正确的定义。 对于polydot,我找到了这个定义。

> polydot
function (degree = 1, scale = 1, offset = 1) 
{
  rval <- function(x, y = NULL) {
    if (!is(x, "vector")) 
        stop("x must be a vector")
    if (!is(y, "vector") && !is.null(y)) 
        stop("y must be a vector")
    if (is(x, "vector") && is.null(y)) {
        (scale * crossprod(x) + offset)^degree
    }
    if (is(x, "vector") && is(y, "vector")) {
        if (!length(x) == length(y)) 
            stop("number of dimension must be the same on both data points")
        (scale * crossprod(x, y) + offset)^degree
     }
  }
  return(new("polykernel", .Data = rval, kpar = list(degree = degree, 
    scale = scale, offset = offset)))
}

但是如果我尝试实现我的代码,它已经给我这个错误:

Fehler in .local(x, ...) : 
   List interface supports only the stringdot kernel. 

至少我尝试了新的距离和多核样式。代码:

function (size = 1) 
{
 kval <- function(x, y = NULL) {
   if (!is(x, "vector")) 
      stop("x must be a vector")
   if (!is(y, "vector") && !is.null(y)) 
      stop("y must be a vector")
   if (is(x, "vector") && is.null(y)) {
      x
   }
if (is(x, "vector") && is(y, "vector")) {
  if (!length(x) == length(y)) 
    stop("number of dimension must be the same on both data points")
  newDist(x,y,size)
  }
 }
 return(new("new_kernel", .Data = kval, kpar = list(size = size)))
}
class(new_kernel) <- "kernel"

具有新的距离功能:

newDist <- function(x, y, xmax) {
  xrow1 <- (as.numeric(rownames(x))) %/% xmax
  yrow1 <- (as.numeric(rownames(y))) %/% xmax
  xrow1 <- (as.numeric(rownames(x))) - (xrow1 * xmax) 
  yrow2 <- (as.numeric(rownames(y))) - (yrow1 * xmax)
  a <- c(xrow1, yrow1)
  b <- c(xrow2, yrow2)
  distance <- dist(rbind(a, b), method = "euclidean")
  newDistance <- distance[1]
  return(newDistance)
}

谢谢您的帮助!

0 个答案:

没有答案