0

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

我使用了 R SVM(准确地说,我使用了来自拨浪鼓的 KSVM)并且我想获得平面的函数(基于变量的权重)以在其他数据系统中使用该函数。任何想法?

谢谢!

4

2 回答 2

1

我也为此苦苦挣扎了很久!终于找到了答案,我将使用kernlabpackage 来演示一个两类案例:

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))

因此,现在,如果您有任何新输入,只需对其进行缩放、转置并插入您的f(x),然后g(x)即可获得其类——这两个函数就是您的 SVM 分类器。

于 2015-08-03T09:27:30.113 回答
0
  1. 打开 RStudio 并调用拨浪鼓。
  2. 使用 GUI 制作模型。
  3. 有一个日志选项卡(最右边的一个)。该选项卡包含您使用 GUI 完成的所有 R 命令。
  4. 查找并复制命令并在需要的地方使用。
于 2014-10-05T09:13:19.867 回答