簡體   English   中英

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

[英]how do I get the function of my SVM model in R?

我有一個要使用13個變量預測的二進制標簽。

我使用過R SVM(確切地說,我從吵鬧聲中使用了KSVM),並且我想獲取平面的功能(基於變量的權重)以在其他數據系統中使用該功能。 任何想法?

謝謝!

我為此也掙扎了很長時間! 終於找到了答案,我將針對兩類情況使用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))

因此,現在,如果您有任何新輸入,只需對其進行縮放,轉置並插入f(x) ,然后插入g(x)以獲得其類-這兩個函數就是您的SVM分類器。

  1. 打開RStudio並發出嘎嘎聲。
  2. 使用GUI做模型。
  3. 有一個日志選項卡(最右邊的一個)。 該選項卡包含您使用GUI所完成的所有R指令。
  4. 查找並復制命令並在需要的地方使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM