如何计算R包LiblineaR中的支持向量数量

时间:2018-03-28 15:06:57

标签: r svm liblinear

例如,有没有办法计算此分类模型中支持向量的数量?

library(LiblineaR)
data(iris)
attach(iris)

x <- iris[,1:4]
y <- factor(iris[, 5])
set.seed(1)
train <- sample(1:dim(iris)[1],100)
detach(iris)

xTrain <- x[train,]
xTest <- x[-train,]
yTrain <- y[train]
yTest <- y[-train]

s <- scale(xTrain, center=T, scale=T)

m <- LiblineaR(data=s, target=yTrain, type=5, cost=0.1)
m

输出

$TypeDetail
[1] "L1-regularized L2-loss support vector classification (L1R_L2LOSS_SVC)"

$Type
[1] 5

$W
           Sepal.Length Sepal.Width Petal.Length Petal.Width       Bias
setosa                0   0.2075367   -0.9154018    0.000000 -0.4105989
versicolor            0  -0.4238142    0.0303085    0.000000 -0.2447197
virginica             0   0.0000000    0.0000000    1.183732 -0.6795709

$Bias
[1] 1

$ClassNames
[1] setosa     versicolor virginica 
Levels: setosa versicolor virginica

$NbClass
[1] 3

attr(,"class")
[1] "LiblineaR"

1 个答案:

答案 0 :(得分:1)

liblineaR包不会在模型输出中存储支持向量,但像kernlab这样的包会这样做:

> library kernlab
> your.svm <- ksvm(s,yTrain,type="C-svc",kernel="polydot", 
      prob.model=TRUE,kpar=list(degree=1, scale=1, offset=0))

您可以访问支持向量的数量,如下所示:

> your.svm@nSV
[1] 23

或更改参数以运行不同类型的SVM:

> your.svm <- ksvm(s,yTrain,type="one-svc", 
     kernel="splinedot",prob.model=TRUE)
 Setting default kernel parameters  
> your.svm@nSV
[1] 22

your.svm对象中存储了许多不同的值 - 类型str(your.svm)以查看它们。此外,以下是您可以使用的所有type

C-svc - C classification
nu-svc - nu classification
C-bsvc - bound-constraint svm classification
spoc-svc - Crammer, Singer native multi-class
kbb-svc - Weston, Watkins native multi-class
one-svc - novelty detection
eps-svr - epsilon regression
nu-svr - nu regression
eps-bsvr - bound-constraint svm regression