How to write a new kernel in ksvm of kernlab with more than 2 parameters?
How to write a new kernel in ksvm of kernlab with more than 2 parameters?
I want to write a new kernel with a new distance function. But I can't find the right definition.
For polydot I found this definition.
> 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)))
but if I try to implement my code it give me already this error :
Fehler in .local(x, ...) :
List interface supports only the stringdot kernel.
At least I tried it with a new distance and the style of polykernel. The code:
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"
with the new distance function :
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)
Thank you for help!
0
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Welcome to Stack Overflow! When assigning a tag for your question, read the tag's description (it is shown when you hover over the tag). You use "kernel" tag, because there is such term in your area. But the meaning of the tag is "Operating System kernel", and this is definitely not your case. Choosing proper tags affects on the audience which will find your question and be able to answer it. I have found your question by "kernel" tag, but I have no knowledge in your area. You may fix tags of your question by edit it. Possibly, "kernel-density" will be the proper tag.
– Tsyvarev
Sep 11 '18 at 17:18