R 和 WEKA 上的支持向量机
Support Vector Machine on R and WEKA
我的数据在 e1071 包的 R 上使用 svm 生成了奇怪的结果,所以我尝试检查 R svm 是否可以生成与 WEKA(或 python)相同的结果,因为我一直在使用 WEKA过去。
我用谷歌搜索了这个问题,发现了一个与我有完全相同困惑但没有答案的问题。 This is the question.
所以我希望我能在这里得到答案。
为了方便起见,我还使用了鸢尾花数据集,并使用整个鸢尾花数据训练了一个模型(WEKA 中的 SMO,以及 R 包 e1071 中的 svm),并对其进行了测试。
WEKA 参数:
weka.classifiers.functions.SMO -C 1.0 -L 0.001 -P 1.0E-12 -N 0 -V 10 -W 1 -K "weka.classifiers.functions.supportVector.RBFKernel -G 0.01 -C 250007"
除默认外,我将内核更改为 RBFKernel 以使其与 R 功能一致。
结果是:
a b c <-- classified as
50 0 0 | a = Iris-setosa
0 46 4 | b = Iris-versicolor
0 7 43 | c = Iris-virginica
R 脚本:
library(e1071)
model <- svm(iris[,-5], iris[,5], kernel="radial", epsilon=1.0E-12)
res <- predict(model, iris[,-5])
table(pred = res, true = iris[,ncol(iris)])
结果是:
true
pred setosa versicolor virginica
setosa 50 0 0
versicolor 0 48 2
virginica 0 2 48
我不是机器学习人员,所以我猜这两种方法的默认参数有很大不同。例如,e1071 的默认值为 0.01 epsilon
,而 WEKA 的默认值为 1.0E-12。我试着翻阅了手册,想让所有的参数都一样,但是很多参数好像都比不上我。
谢谢。
SMO的RWeka参数参考http://weka.sourceforge.net/doc.dev/weka/classifiers/functions/SMO.html,使用?svm查找e1071 svm实现的对应参数
根据 ?svm,R e1071 svm 是 libsvm 的接口,似乎使用标准 QP 求解器。
For multiclass-classification with k levels, k>2, libsvm uses the
‘one-against-one’-approach, in which k(k-1)/2 binary classifiers are
trained; the appropriate class is found by a voting scheme.
libsvm internally uses a sparse data representation, which is also high-level supported by the package SparseM.
RWeka 中的 SMO 相反
implements John C. Platt's sequential minimal optimization algorithm
for training a support vector classifier using polynomial or RBF
kernels. Multi-class problems are solved using pairwise
classification.
所以,这两个实现大体上是不同的(所以结果可能会有点不同)。仍然如果我们选择相同的相应超参数,混淆矩阵几乎相同:
library(RWeka)
model.smo <- SMO(Species ~ ., data = iris,
control = Weka_control(K = list("RBFKernel", G=2), C=1.0, L=0.001, P=1.0E-12, N=0, V=10, W=1234))
res.smo <- predict(model.smo, iris[,-5])
table(pred = res.smo, true = iris[,ncol(iris)])
true
pred setosa versicolor virginica
setosa 50 0 0
versicolor 0 47 1
virginica 0 3 49
library(e1071)
set.seed(1234)
model.svm <- svm(iris[,-5], iris[,5], kernel="radial", cost=1.0, tolerance=0.001, epsilon=1.0E-12, scale=TRUE, cross=10)
res.svm <- predict(model.svm, iris[,-5])
table(pred = res.svm, true = iris[,ncol(iris)])
true
pred setosa versicolor virginica
setosa 50 0 0
versicolor 0 49 1
virginica 0 1 49
也参考这个:[https://stats.stackexchange.com/questions/130293/svm-and-smo-main-differences][1] and this [https://www.quora.com/Whats-the-difference-between-LibSVM-and-LibLinear][1]
我的数据在 e1071 包的 R 上使用 svm 生成了奇怪的结果,所以我尝试检查 R svm 是否可以生成与 WEKA(或 python)相同的结果,因为我一直在使用 WEKA过去。
我用谷歌搜索了这个问题,发现了一个与我有完全相同困惑但没有答案的问题。 This is the question.
所以我希望我能在这里得到答案。
为了方便起见,我还使用了鸢尾花数据集,并使用整个鸢尾花数据训练了一个模型(WEKA 中的 SMO,以及 R 包 e1071 中的 svm),并对其进行了测试。
WEKA 参数:
weka.classifiers.functions.SMO -C 1.0 -L 0.001 -P 1.0E-12 -N 0 -V 10 -W 1 -K "weka.classifiers.functions.supportVector.RBFKernel -G 0.01 -C 250007"
除默认外,我将内核更改为 RBFKernel 以使其与 R 功能一致。
结果是:
a b c <-- classified as
50 0 0 | a = Iris-setosa
0 46 4 | b = Iris-versicolor
0 7 43 | c = Iris-virginica
R 脚本:
library(e1071)
model <- svm(iris[,-5], iris[,5], kernel="radial", epsilon=1.0E-12)
res <- predict(model, iris[,-5])
table(pred = res, true = iris[,ncol(iris)])
结果是:
true
pred setosa versicolor virginica
setosa 50 0 0
versicolor 0 48 2
virginica 0 2 48
我不是机器学习人员,所以我猜这两种方法的默认参数有很大不同。例如,e1071 的默认值为 0.01 epsilon
,而 WEKA 的默认值为 1.0E-12。我试着翻阅了手册,想让所有的参数都一样,但是很多参数好像都比不上我。
谢谢。
SMO的RWeka参数参考http://weka.sourceforge.net/doc.dev/weka/classifiers/functions/SMO.html,使用?svm查找e1071 svm实现的对应参数
根据 ?svm,R e1071 svm 是 libsvm 的接口,似乎使用标准 QP 求解器。
For multiclass-classification with k levels, k>2, libsvm uses the ‘one-against-one’-approach, in which k(k-1)/2 binary classifiers are trained; the appropriate class is found by a voting scheme. libsvm internally uses a sparse data representation, which is also high-level supported by the package SparseM.
RWeka 中的 SMO 相反
implements John C. Platt's sequential minimal optimization algorithm for training a support vector classifier using polynomial or RBF kernels. Multi-class problems are solved using pairwise classification.
所以,这两个实现大体上是不同的(所以结果可能会有点不同)。仍然如果我们选择相同的相应超参数,混淆矩阵几乎相同:
library(RWeka)
model.smo <- SMO(Species ~ ., data = iris,
control = Weka_control(K = list("RBFKernel", G=2), C=1.0, L=0.001, P=1.0E-12, N=0, V=10, W=1234))
res.smo <- predict(model.smo, iris[,-5])
table(pred = res.smo, true = iris[,ncol(iris)])
true
pred setosa versicolor virginica
setosa 50 0 0
versicolor 0 47 1
virginica 0 3 49
library(e1071)
set.seed(1234)
model.svm <- svm(iris[,-5], iris[,5], kernel="radial", cost=1.0, tolerance=0.001, epsilon=1.0E-12, scale=TRUE, cross=10)
res.svm <- predict(model.svm, iris[,-5])
table(pred = res.svm, true = iris[,ncol(iris)])
true
pred setosa versicolor virginica
setosa 50 0 0
versicolor 0 49 1
virginica 0 1 49
也参考这个:[https://stats.stackexchange.com/questions/130293/svm-and-smo-main-differences][1] and this [https://www.quora.com/Whats-the-difference-between-LibSVM-and-LibLinear][1]