0

I am trying to understand the way the e1071 package obtains its SVM predictions in a two-class classification framework. Consider the following toy example.

library(mvtnorm)
library(e1071)

n <- 50

### Gaussians
eps <- 0.05
data1 <- as.data.frame(rmvnorm(n, mean = c(0,0), sigma=diag(rep(eps,2))))
data2 <- as.data.frame(rmvnorm(n, mean = c(1,1), sigma=diag(rep(eps,2))))

### Train Model
data_df <- as.data.frame(rbind(data1, data2))
data <- as.matrix(data_df)
data_df$y <- as.factor(c(rep(-1,n), rep(1,n)))

svm <- svm(y ~ ., data = data_df, kernel = "radial", gamma=1, type = "C-classification", scale = FALSE)

Having trained the SVM, I would like to write a function that uses the coefficients and the intercept to predict on a new data point.

Recall that the kernel trick guarantees that we can write the prediction on a new point as the weighted sum of the kernel evaluated at the support vectors and the new point itself (plus some intercept).

In other words: How to combine the following three terms

supportv <- svm$SV
coefs <- svm$coefs
intercept <- svm$rho

to get the prediction associated with the corresponding SVM?

If this is not possible, or too complicated, I would also switch to a different package.

Claudio Moneo
  • 489
  • 1
  • 4
  • 10

0 Answers0