R SVM 阿尔法系数 [英] R SVM alpha coefficients

查看:31
本文介绍了R SVM 阿尔法系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 PMML 文件在 R 中重新创建 SVM 对象,但我无法理解 R 如何存储 alpha 系数.我目前正在 iris 数据集上测试它,并使用命令

I am trying to recreate a SVM object in R from a PMML file, but am having trouble understanding how R stores the alpha coefficients. I am currently testing it on the iris data set and I generated a R SVM object with the command

library(e1071)
data(iris)
model<-svm(Species~.,data=iris)

我正在用命令查看它的系数

and I am looking at its coefficients with the command

model$coefs

得到如下结果

            [,1]        [,2]
 [1,]  0.0890967  0.00000000
 [2,]  0.0000000  0.14547777
 [3,]  0.8651998  0.94869969
 [4,]  0.0000000  0.13152589
 [5,]  0.0000000  0.27612243
 [6,]  0.8421469  0.45912899
 [7,]  0.4785865  0.00000000
 [8,]  1.0000000  1.00000000
 [9,] -0.4941407  1.00000000
[10,]  0.0000000  1.00000000
[11,]  0.0000000  0.63848160
[12,]  0.0000000  1.00000000
[13,]  0.0000000  1.00000000
[14,] -0.5471576  0.00000000
[15,]  0.0000000  0.52796849
[16,] -0.3772321  0.49504241
[17,]  0.0000000  1.00000000
[18,]  0.0000000  1.00000000
[19,] -0.1146136  1.00000000
[20,]  0.0000000  1.00000000
[21,]  0.0000000  1.00000000
[22,]  0.0000000  1.00000000
[23,]  0.0000000  1.00000000
[24,]  0.0000000  1.00000000
[25,]  0.0000000  1.00000000
[26,]  0.0000000  1.00000000
[27,] -0.7418858  0.10024212
[28,]  0.0000000  1.00000000
[29,]  0.0000000  0.60104219
[30,] -1.0000000  0.00000000
[31,] -0.8335805 -1.00000000
[32,]  0.0000000 -0.05538514
[33,]  0.0000000 -1.00000000
[34,]  0.0000000 -1.00000000
[35,] -0.6171002  0.00000000
[36,] -0.3564736 -1.00000000
[37,]  0.0000000 -1.00000000
[38,]  0.0000000 -1.00000000
[39,]  0.0000000 -1.00000000
[40,]  0.0000000 -1.00000000
[41,]  0.0000000 -1.00000000
[42,]  0.0000000 -1.00000000
[43,] -0.6609450 -0.78275762
[44,]  0.0000000 -1.00000000
[45,]  0.0000000 -1.00000000
[46,]  0.0000000 -1.00000000
[47,]  0.0000000 -1.00000000
[48,]  0.0000000 -0.52463404
[49,]  0.0000000 -1.00000000
[50,] -0.4928554  0.00000000
[51,]  0.0000000 -1.00000000

据我所知,有 51 个支持向量,并且由于 R 对多类 SVM 使用一对一,因此基本上有 3 个分类器(setosa v. versicolor、setosa v. virginica 和 versicolor v. virginica)分别使用这些向量的一个子集.我怎么知道这个 coefs 列表中的哪些系数对应于哪个分类器(以及每个分类器使用哪些支持向量)?

To my understanding, there are 51 support vectors and since R uses one versus one for multi-class SVM, there are essentially 3 classifiers (setosa v. versicolor, setosa v. virginica, and versicolor v. virginica) that each use a subset of these vectors. How do I know which coefficients in this coefs list correspond to which classifier (and which support vectors are used by each classifier)?

我看到 model$nSV 告诉你每个分类器中有多少支持向量,但它没有指定哪些支持向量实际上是分类器的一部分.提前致谢.

I saw that model$nSV tells you how many support vectors are in each classifier, but it does not specify which support vectors are actually part of the classifier. Thanks in advance.

推荐答案

是的,libsvm(r 使用的)保持支持向量的方式有点神秘".为了更好地理解,让我们只使用花瓣特征,以便我们稍后对其进行可视化.

Yes, the way that libsvm (which r uses) keeps the support vector is a bit "cryptic". To understand better, let's use only Petal features, so we can visualize it later.

library(e1071)
data(iris)

fit=svm(Species~Petal.Length+Petal.Width, data=iris, kernel = "linear", cost = 10, scale=F)

alphas 时间 ys"存储在 coef 矩阵中.要知道每个类有多少 SV 相关,您必须查看:

The "alphas time ys" are stored in the coef matrix. To know how many SV relate to each class, you have to look at:

n = fit$nSV; n

在我的运行中有 1、8 和 8.这意味着第一个 n[1] (1) SV 仅与第一类相关.对于前 n[1] 行,列是 1vs2、1vs3.对于接下来的 n[2] 行,列是 2vs1、2vs3.等等.请注意,某些值可能为 0.在我的运行中,第 2 类-第 1 列中的 7/8 个值是 0,因为您只需要 1 个点来分隔第 1 类和第 2 类.

In my run there are 1, 8 and 8. This means that the first n[1] (1) SV relate only to the first class. For the first n[1] rows the columns are 1vs2, 1vs3. For the next n[2] rows the columns are 2vs1, 2vs3. Etc. Note that some values might be 0. In my run, 7/8 values in class 2- column 1 are 0, since you only need 1 point to separate class 1 and 2.

如果我们想提取 1 对 3 分离平面,我们需要这样做:

If we want to extract the 1 vs. 3 separation planes, we need to do it as follows:

# class 1 vs. 3
# class 1 has n[1] SV, class 3 has n[3]
# rows of n[1], column 2 = [1vs2, 1vs3*]
# rows of n[3], column 1 = [3vs1*, 3vs2]
coef1 = c(fit$coefs[1:n[1],2],fit$coefs[(sum(n[1:2])+1):sum(n),1])
SVs1 = rbind(fit$SV[1:n[1],],fit$SV[(sum(n[1:2])+1):sum(n),])
w1 = t(SVs1)%*%coef1
# rho stores the b's, [1vs2, 1vs3, 2vs3]
b1 = -fit$rho[2]

绘图(仅第 1 类和第 3 类):

Plotting (only class 1 and 3):

plot(rbind(iris.norm[1:50,],iris.norm[101:150,]), col=iris$Species)
abline(-b1/w1[2], -w1[1]/w1[2], col=4)

图片:

这篇关于R SVM 阿尔法系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆