如何在R中获得SVM模型的功能?

时间:2022-08-28 07:30:57

I have a binary tag I'm trying to predict with 13 variables.

我有一个二进制标记,我试图用13个变量进行预测。

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?

我已经使用了R SVM(要精确地使用KSVM来解决)并且我希望获得平面的功能(基于变量的权重)以在其他数据系统中使用该功能。任何想法?

thanks!

谢谢!

2 个解决方案

#1


1  

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:

我也挣扎了很久了!终于找到了答案,我将使用kernlab包证明一个两类案例:

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.

所以现在,如果你有任何新的输入,只需缩放它,转置并插入f(x),然后g(x)得到它的类 - 这两个函数是你的SVM分类器。

#2


0  

  1. Open RStudio and call rattle.
  2. 打开RStudio并调用拨浪鼓。
  3. do your model using GUI.
  4. 使用GUI完成您的模型。
  5. There is a log tab (the right most one). That tab contains all the R comands corresponding to which you have done using the GUI.
  6. 有一个日志选项卡(最右边的一个)。该选项卡包含您使用GUI完成的所有R命令。
  7. Find and Copy the comands and use wherever required.
  8. 查找并复制命令并在需要的地方使用。

#1


1  

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:

我也挣扎了很久了!终于找到了答案,我将使用kernlab包证明一个两类案例:

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.

所以现在,如果你有任何新的输入,只需缩放它,转置并插入f(x),然后g(x)得到它的类 - 这两个函数是你的SVM分类器。

#2


0  

  1. Open RStudio and call rattle.
  2. 打开RStudio并调用拨浪鼓。
  3. do your model using GUI.
  4. 使用GUI完成您的模型。
  5. There is a log tab (the right most one). That tab contains all the R comands corresponding to which you have done using the GUI.
  6. 有一个日志选项卡(最右边的一个)。该选项卡包含您使用GUI完成的所有R命令。
  7. Find and Copy the comands and use wherever required.
  8. 查找并复制命令并在需要的地方使用。