Reputation: 191
I have a binary tag I'm trying to predict with 13 variables.
I've used R SVM ( to be precised I used KSVM from rattle) and I want to get the function of the plane (the weights based on the variables) to use that function in other data systems. any idea?
thanks!
Upvotes: 0
Views: 1292
Reputation: 798
I struggled with this too for a long while! Finally found the answer, which I'll demonstrate using kernlab
package for a two-class case:
library(kernlab)
# set seed to make this reproducible
set.seed(101)
# create a matrix of inputs
x <- rbind(matrix(rnorm(120),,2),matrix(rnorm(120,mean=3),,2)); head(x)
# create an output...something simple and contrived
y <- matrix(c(rep(1,60),rep(-1,60))); head(y); tail(y)
# train svm model
our_svm <- ksvm(x,y,type="C-svc")
# if you want to plot classification results, run plot(our_svm,data=x)
# get the weights of the classifier
(w <- colSums(coef(our_svm)[[1]] * x[unlist(alphaindex(our_svm)),]))
# get the intercept
(b <- b(our_svm))
# our classifier takes the form g(x) = sign(f(x)),
# where f(x) = w*x + b, and input variables x are SCALED AND TRANSPOSED
# scale it
x_sc <- scale(x); head(x_sc)
# get the f(x) (don't forget to transpose x!)
(f_x <- colSums(t(x_sc)*w) + b)
# get the sign, which is the class of the inputs
(g_x <- sign(f_x))
# if we run fitted(our_svm), we'll see it came up with the same results
# as our manual calculations
table(Manual_Calc = g_x, From_Model = fitted(our_svm))
So now, if you have any new input, just scale it, transpose and plug into your f(x), and then g(x) to get its class - these two functions are your SVM classifier.
Upvotes: 1
Reputation: 519
Upvotes: 0