实验报告支持向量机
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验报告支持向量机
实验报告支持向量机
实验原理:支持向量机的原理和实现技术。
实验题目:对鸢尾花数据利用SVM技术进行分类预测。
实验要求:把鸢尾花数据分成训练集和测试集,然后针对样本SVM预测分类。
实验题目--分析报告:
data(iris)
> rm(list=ls())
> gc()
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 252143 13.5 608394 32.5 410515 22.0
Vcells 528486 4.1 8388608 64.0 1606736 12.3
> library(MASS)
> data(iris)
> library(e1071)
> summary(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 Median :5.800 Median :3.000 Median :4.350 Median :1.300 Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800 Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500 Species
setosa :50
versicolor:50
virginica :50
仅选择Petal.Length和Petal.Width这两个特征时
> model <- svm( Petal.Length ~ Petal.Width, data = iris) > print(model)
Call:
svm(formula = Petal.Length ~ Petal.Width, data = iris) Parameters:
SVM-Type: eps-regression
SVM-Kernel: radial
cost: 1
gamma: 1
epsilon: 0.1
Number of Support Vectors: 80
> summary(model)
Call:
svm(formula = Petal.Length ~ Petal.Width, data = iris) Parameters:
SVM-Type: eps-regression
SVM-Kernel: radial
cost: 1
gamma: 1
epsilon: 0.1
Number of Support Vectors: 80
> svm(formula = Petal.Length ~ Petal.Width, data = iris)
Call:
svm(formula = Petal.Length ~ Petal.Width, data = iris) Parameters:
SVM-Type: eps-regression
SVM-Kernel: radial
cost: 1
gamma: 1
epsilon: 0.1
Number of Support Vectors: 80
> predict(model,iris)
1 2 3 4 5 6 7 8 1.423837 1.423837 1.423837 1.423837 1.423837 1.616469 1.477096 1.423837 9 10 11 12 13 14 15 16 1.423837 1.456913 1.423837 1.423837 1.456913 1.456913 1.423837 1.616469 17 18 19 20 21 22 23 24 1.616469 1.477096 1.477096 1.477096 1.423837 1.616469 1.423837 1.834129 25 26 27 28 29 30 31 32 1.423837 1.423837 1.616469 1.423837 1.423837 1.423837 1.423837 1.616469 33 34 35 36 37 38 39 40 1.456913 1.423837 1.423837 1.423837 1.423837 1.456913 1.423837 1.423837 41 42 43 44 45 46 47 48 1.477096 1.477096 1.423837 2.115572 1.616469 1.477096 1.423837 1.423837 49 50 51 52 53 54 55 56 1.423837 1.423837 4.497139 4.675747 4.675747 4.293120 4.675747 4.293120 57 58 59 60 61 62 63 64 4.836986 3.477178 4.293120 4.497139 3.477178 4.675747
3.477178
4.497139 65 66 67 68 69 70 71 72 4.293120 4.497139
4.675747 3.477178 4.675747 3.784514
5.134367 4.293120
73 74 75 76 77 78 79 80 4.675747 4.056829 4.293120 4.497139 4.497139 4.988183 4.675747 3.477178 81 82 83 84 85 86 87 88 3.784514 3.477178 4.056829 4.836986 4.675747 4.836986 4.675747 4.293120 89 90 91 92 93 94 95 96 4.293120 4.293120 4.056829 4.497139 4.056829 3.477178 4.293120 4.056829 97 98 99 100 101 102 103 104 4.293120 4.293120 3.784514 4.293120 5.752344 5.277177 5.540312 5.134367 105 106 107 108 109 110 111 112 5.646354 5.540312 4.988183 5.134367 5.134367 5.752344 5.414423 5.277177 113 114 115 116 117 118 119 120 5.540312 5.414423 5.760448 5.722808 5.134367 5.646354 5.722808 4.675747 121 122 123 124 125 126 127 128 5.722808 5.414423 5.414423 5.134367 5.540312 5.134367 5.134367 5.134367 129 130 131 132 133 134 135 136 5.540312
4.836986
5.277177 5.414423 5.646354 4.675747 4.497139
5.722808 137 138 139 140 141 142 143 144 5.760448 5.134367
5.134367 5.540312 5.760448 5.722808 5.277177 5.722808 145 146 147 148 149 150
5.752344 5.722808 5.277177 5.414423 5.722808 5.134367
分割数据集
> set.seed(2)
> test=sample(1:nrow(iris),100)
> iris.train<-iris[-test,]
> iris.test<-iris[test,]
> dim(iris.train);dim(iris.test)
[1] 50 5
[1] 100 5
> model <- svm(Petal.Length ~ Petal.Width, data = iris.train) > prediction <- predict(model, iris.test[,-1])
> tab <- table(pred = prediction, true = iris.test[,1])
> tab
true
pred 4.3 4.4 4.6 4.8 4.9 5 5.1 5.2 5.4 5.5 5.6 5.7 5.8 5.9 6
1.47642618523351 0 1 2 2 1 4 2 2 2 1 0 0 1 0 0
1.51493452501901 1 0 0 1 2 0 0 1 0 0 0 0 0 0 0
1.53779430619952 0 0 0 1 0 1 2 0 0 0 0 1 0 0 0
1.69675064955348 0 0 0 0 0 1 1 0 3 0 0 0 0 0 0
1.94050318790796 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
2.24776679658417 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
3.59332505159476 0 0 0 0 1 1 0 0 0 1 0 1 0 0 1
3.85716957677727 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0
4.07568831142215 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0
4.25427922933862 0 0 0 0 0 0 0 0 0 0 2 2 0 0 0
4.40410985410782 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
4.53963900708948 0 0 0 0 0 0 0 0 1 0 1 0 0 1 2
4.67568885604792 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
4.82439822095048 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
4.99249624342828 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5.1794222728005 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0
5.37678640997714 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
5.56945420033049 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5.73817028383457 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5.86321818426491 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5.92371940451556 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5.92829837440344 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
true
pred 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.2 7.3 7.4 7.7 7.9 1.47642618523351 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.51493452501901 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.53779430619952 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1.69675064955348 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1.94050318790796 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2.24776679658417 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3.59332505159476 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3.85716957677727 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4.07568831142215 1 0 0 0 0 0 0 0 0 0 0 0 0 0 4.25427922933862 0 0 1 1 0 1 0 0 0 0 0 0 0 0 4.40410985410782 3 0 0 0 0 1 0 0 0 0 0 0 0 0 4.53963900708948 0 0 1 1 0 0 1 0 0 0 0 0 0 0 4.67568885604792 0 0 1 0 0 0 0 0 0 1 0 0 0 0 4.82439822095048 0 0 0 0 0 0 1 0 0 0 0 0 0 0
4.99249624342828 1 1 0 1 1 0 1 0 0 1 1 0 0 0
5.1794222728005 0 0 1 0 0 0 0 0 0 0 0 1 0 0 5.37678640997714 0 0 0 0 0 0 0 0 0 0 0 0 0 1 5.56945420033049 0 0 0 0 0 0 0 1 1 0 0 0 0 0 5.73817028383457 0 0 0 0 1 0 0 0 0 0 0 0 1 0 5.86321818426491 0 1 0 1 0 0 0 1 2 0 0 0 1 0 5.92371940451556 0 0 1 0 0 0 1 0 0 0 0 0 0 0 5.92829837440344 0 0 1 0 0 0 1 0 0 0 0 0 0 0 > classAgreement(tab)
$`diag`
[1] 0.02
$kappa
[1] -0.01554404
$rand
[1] 0.910101
$crand
[1] 0.05377635
> tuned <- tune.svm(Petal.Length ~ Petal.Width, data = iris.train, gamma = 10^(-6:-1),
+ cost = 10^(1:2))
> summary(tuned)
Parameter t uning of ‘svm’:
- sampling method: 10-fold cross validation
- best parameters:
gamma cost
0.1 10
- best performance: 0.1490541
- Detailed performance results:
gamma cost error dispersion
1 1e-06 10 3.4722488 2.2064541
2 1e-05 10 3.4280844 2.1802703
3 1e-0
4 10 3.0083324 1.9319450
4 1e-03 10 0.7356553 0.4432716
5 1e-02 10 0.1751669 0.1367145
6 1e-01 10 0.1490541 0.1401728
7 1e-06 100 3.4281316 2.1803297
8 1e-05 100 3.0082136 1.9318791
9 1e-04 100 0.7319696 0.4410257
10 1e-03 100 0.1789653 0.1266345
11 1e-02 100 0.1490979 0.1336199
12 1e-01 100 0.1613977 0.1530779。