b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

支持向量机超平面_支持向量机通过寻找_r语言 支持向量机分类(5)

电脑杂谈  发布时间:2017-02-02 17:03:39  来源:网络整理

最后,我们回到最开始的那个手写数字的案例,我们试着利用支持向量机重做这个案例。(这个案例的描述与数据参见《R语言与机器学习学习笔记(分类算法)(1)》)

运行代码:

setwd("D:/R/data/digits/trainingDigits")
names<-list.files("D:/R/data/digits/trainingDigits")
data<-paste("train",1:1934,sep="")
for(i in 1:length(names))
         assign(data[i],as.vector(as.matrix(read.fwf(names[i],widths=rep(1,32)))))
label<-rep(0:9,c(189,198,195,199,186,187,195,201,180,204))
 
data1<-get(data[1])
for(i in 2:length(names))
         data1<-rbind(data1,get(data[i]))
 
m <- svm(data1,label,cross=10,type="C-classification")
m
summary(m)
pred<-fitted(m)
table(pred,label)
 
setwd("D:/R/data/digits/testDigits")
names<-list.files("D:/R/data/digits/testDigits")
data<-paste("train",1:1934,sep="")
for(i in 1:length(names))
         assign(data[i],as.vector(as.matrix(read.fwf(names[i],widths=rep(1,32)))))
data2<-get(data[1])
for(i in 2:length(names))
         data2<-rbind(data2,get(data[i]))
pred<-predict(m,data2)
labeltest<-rep(0:9,c(87,97,92,85,114,108,87,96,91,89))
table(pred,labeltest)

模型摘要:

Call:

svm.default(x = data1, y = label, type ="C-classification", cross =10)

Parameters:

SVM-Type: C-classification

SVM-Kernel: radial

cost: 1

gamma: 0.0009765625

Number of Support Vectors:1139 ( 78 130 101 124 109 122 87 93 135 160 )

Number of Classes: 10

Levels: 0 1 2 3 4 5 6 7 8 9

10-fold cross-validation on training data:

Total Accuracy: 96.7425

Single Accuracies:

97.40933 98.96373 91.7525899.48187 94.84536 94.30052 97.40933 96.90722 98.96373 97.42268

当然,我们还可以通过m$SV查看支持向量的情况,m$index查看支持向量的标签,m$rho查看分类时的截距b。

训练集分类结果:

我们拿测试数据来看:

分类正确率为:0.9735729,误差率为2.6%左右,确实达到了开篇提出的可比的目的,而需要储存的支持向量个数仅为1139个,比原来的训练数据1934个要少了近50%,也达到了我们要求的节约存储的目的。当然得一提的是线性分类的效果在实际中也没有那么糟糕,可以牺牲线性核函数的正确率来换取分类速度与存储空间。另外,支持向量的个数与训练集的出错率也没有特别必然的联系,而是与容错率cost有一定的联系。

在R中使用支持向量机:(3.e1071包和klaR包)

支持向量机: Maximum MarginClassifier


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-30140-5.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 隋

      把叙利亚难民危机与光棍危机进行对冲

    热点图片
    拼命载入中...