日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

支持向量机SVM算法原理及应用(R)

發(fā)布時間:2023/12/13 综合教程 32 生活家
生活随笔 收集整理的這篇文章主要介紹了 支持向量机SVM算法原理及应用(R) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

支持向量機(jī)SVM算法原理及應(yīng)用(R)

2016年08月17日 16:37:25閱讀數(shù):22292更多
個人分類:數(shù)據(jù)挖掘?qū)崙?zhàn)應(yīng)用

版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請注明來源。 https://blog.csdn.net/csqazwsxedc/article/details/52230092

只要接觸到數(shù)據(jù)挖掘/機(jī)器學(xué)習(xí),相比都會聽過“支持向量機(jī)”的大名。在機(jī)器學(xué)習(xí)領(lǐng)域,支持向量機(jī)SVM(Support Vector Machine)是一個有監(jiān)督的學(xué)習(xí)模型,通常用來進(jìn)行模式識別、分類、以及回歸分析。SVM涉及的知識面非常廣,目標(biāo)函數(shù)、優(yōu)化過程、并行方法、算法收斂性、樣本復(fù)雜度等。學(xué)習(xí)SVM可以看《統(tǒng)計學(xué)習(xí)方法》、Andrew Ng支持向量機(jī)等,這里推薦一個博客,講的非常詳細(xì),我就不搬過來了,大家可以直接去看那篇博客,最好能靜下心來直接拿起筆跟著博主推理一邊,這樣更能加深印象。

http://blog.csdn.net/v_july_v/article/details/7624837

學(xué)習(xí)完原理,接下來就該應(yīng)用了,應(yīng)用后更能加深對SVM的理解。

從事漁業(yè)生產(chǎn)有經(jīng)驗的從業(yè)者可通過觀察水色變化調(diào)控水質(zhì),以維持養(yǎng)殖水體生態(tài)系統(tǒng)中浮游植物、微生物類、浮游動物等合理的動態(tài)平衡。由于這些多是通過經(jīng)驗和肉眼觀察進(jìn)行判斷,存在主觀性引起的觀察性偏倚,使觀察結(jié)果的可比性、可重復(fù)性降低,不易推廣應(yīng)用。當(dāng)前,數(shù)字圖像處理技術(shù)為計算機(jī)監(jiān)控技術(shù)在水產(chǎn)養(yǎng)殖業(yè)的應(yīng)用提供更大的空間。在水質(zhì)在線監(jiān)測方面,數(shù)字圖像處理技術(shù)是基于計算機(jī)視覺,以專家經(jīng)驗為基礎(chǔ),對池塘水色進(jìn)行優(yōu)劣分級,達(dá)到對池塘水色的準(zhǔn)確快速判別。

水色分類

水色

淺綠色(清水或濁水)

灰藍(lán)色

黃褐色

茶褐色

(姜黃、茶褐、紅褐、褐中帶綠等)

綠色(黃綠、油綠、藍(lán)綠、墨綠、綠中帶褐等)

水質(zhì)類別

1

2

3

4

5

標(biāo)準(zhǔn)條件下拍攝的水樣圖像

每個水質(zhì)圖片命名規(guī)則為“類別-編號.jpg”,如“1_1.jpg”說明當(dāng)前圖片屬于第1類的樣本。

接下來進(jìn)行特征提取

采用顏色矩來提取水樣圖像的特征

水色圖像特征與相應(yīng)的水色類別的部分?jǐn)?shù)據(jù)

水質(zhì)類別

序號

R通道一階矩

G通道一階矩

B通道一階矩

R通道二階矩

G通道二階矩

B通道二階矩

R通道三階矩

G通道三階矩

B通道三階矩

1

1

0.582823

0.543774

0.252829

0.014192

0.016144

0.041075

-0.01264

-0.01609

-0.04154

2

1

0.495169

0.539358

0.416124

0.011314

0.009811

0.014751

0.015367

0.01601

0.019748

3

1

0.510911

0.489695

0.186255

0.012417

0.010816

0.011644

-0.00747

-0.00768

-0.00509

4

1

0.420351

0.436173

0.167221

0.01122

0.007195

0.010565

-0.00628

0.003173

-0.00729

5

1

0.211567

0.335537

0.111969

0.012056

0.013296

0.00838

0.007305

0.007503

0.00365

1

2

0.563773

0.534851

0.271672

0.009723

0.007856

0.011873

-0.00513

0.003032

-0.00547

2

2

0.465186

0.508643

0.361016

0.013753

0.012709

0.019557

0.022785

0.022329

0.031616

3

2

0.533052

0.506734

0.185972

0.011104

0.007902

0.01265

0.004797

-0.0029

0.004214

4

2

0.398801

0.42556

0.191341

0.014424

0.010462

0.01547

0.009207

0.006471

0.006764

5

2

0.298194

0.427725

0.097936

0.014778

0.012456

0.008322

0.00851

0.006117

0.00347

1

3

0.630328

0.594269

0.298577

0.007731

0.005877

0.010148

0.003447

-0.00345

-0.00653

2

3

0.491916

0.546367

0.425871

0.010344

0.008293

0.01226

0.009285

0.009663

0.011549

3

3

0.559437

0.522702

0.194201

0.012478

0.007927

0.012183

0.004477

-0.00341

-0.00529

4

3

0.402068

0.431443

0.177364

0.010554

0.007287

0.010748

0.006261

-0.00341

0.006419

5

3

0.408963

0.486953

0.178113

0.012662

0.009752

0.014497

-0.00672

0.002168

0.009992

1

4

0.638606

0.61926

0.319711

0.008125

0.006045

0.009746

-0.00487

0.003083

-0.0045

SVM預(yù)測模型輸入變量

序號

變量名稱

變量描述

取值范圍

1

R通道一階矩

水樣圖像在R顏色通道的一階矩

0~1

2

G通道一階矩

水樣圖像在G顏色通道的一階矩

0~1

3

B通道一階矩

水樣圖像在B顏色通道的一階矩

0~1

4

R通道二階矩

水樣圖像在R顏色通道的二階矩

0~1

5

G通道二階矩

水樣圖像在G顏色通道的二階矩

0~1

6

B通道二階矩

水樣圖像在B顏色通道的二階矩

0~1

7

R通道三階矩

水樣圖像在R顏色通道的三階矩

-1~1

8

G通道三階矩

水樣圖像在G顏色通道的三階矩

-1~1

9

B通道三階矩

水樣圖像在B顏色通道的三階矩

-1~1

10

水質(zhì)類別

不同類別能表征水中浮游植物的種類和多少

1,2,3,4,5

圖形轉(zhuǎn)換為數(shù)據(jù)后,R語言建模

建模之前,我來介紹下R語言的SVM用法。使用SVM需安裝e1071包

第一種簡單方式建模:

svm(formula, data= NULL, subset, na.action = na.omit , scale= TRUE)

formula:函數(shù)模型的形式

data:模型中包含的有變量的一組可選格式數(shù)據(jù)。

參數(shù)na.action用于指定當(dāng)樣本數(shù)據(jù)中存在無效的空數(shù)據(jù)時系統(tǒng)應(yīng)該進(jìn)行的處理。默認(rèn)值na.omit表明程序會忽略那些數(shù)據(jù)缺失的樣本。另外一個可選的賦值是na.fail,它指示系統(tǒng)在遇到空數(shù)據(jù)時給出一條錯誤信息。

參數(shù)scale為一個邏輯向量,指定特征數(shù)據(jù)是否需要標(biāo)準(zhǔn)化(默認(rèn)標(biāo)準(zhǔn)化為均值0,方差1)。索引向量subset用于指定那些將被來訓(xùn)練模型的采樣數(shù)據(jù)。

第二種根據(jù)所給的數(shù)據(jù)建模:

svm(x, y = NULL, scale = TRUE, type = NULL, kernel = "radial",degree = 3, gamma = if (is.vector(x)) 1 else 1 / ncol(x),coef0 = 0, cost = 1, nu = 0.5, subset, na.action = na.omit)

此處,x可以是一個數(shù)據(jù)矩陣,也可以是一個數(shù)據(jù)向量,同時也可以是一個稀疏矩陣。y是對于x數(shù)據(jù)的結(jié)果標(biāo)簽,它既可以是字符向量也可以為數(shù)值向量。x和y共同指定了將要用來建模的訓(xùn)練數(shù)據(jù)以及模型的基本形式。
參數(shù)type用于指定建立模型的類別。支持向量機(jī)模型通常可以用作分類模型、回歸模型或者異常檢測模型。根據(jù)用途的差異,在svm()函數(shù)中的type可取的值有C-classification、nu-classification、one-classification、eps-regression和nu-regression這五種類型中。其中,前三種是針對于字符型結(jié)果變量的分類方式,其中第三種方式是邏輯判別,即判別結(jié)果輸出所需判別的樣本是否屬于該類別;而后兩種則是針對數(shù)值型結(jié)果變量的分類方式。
此外,kernel是指在模型建立過程中使用的核函數(shù)。針對線性不可分的問題,為了提高模型預(yù)測精度,通常會使用核函數(shù)對原始特征進(jìn)行變換,提高原始特征維度,解決支持向量機(jī)模型線性不可分問題。svm()函數(shù)中的kernel參數(shù)有四個可選核函數(shù),分別為線性核函數(shù)、多項式核函數(shù)、高斯核函數(shù)及神經(jīng)網(wǎng)絡(luò)核函數(shù)。其中,高斯核函數(shù)與多項式核函數(shù)被認(rèn)為是性能最好、也最常用的核函數(shù)。
核函數(shù)有兩種主要類型:局部性核函數(shù)和全局性核函數(shù),高斯核函數(shù)是一個典型的局部性核函數(shù),而多項式核函數(shù)則是一個典型的全局性核函數(shù)。局部性核函數(shù)僅僅在測試點附近小領(lǐng)域內(nèi)對數(shù)據(jù)點有影響,其學(xué)習(xí)能力強、泛化性能較弱;而全局性核函數(shù)則相對來說泛化性能較強、學(xué)習(xí)能力較弱。
對于選定的核函數(shù),degree參數(shù)是指核函數(shù)多項式內(nèi)積函數(shù)中的參數(shù),其默認(rèn)值為3。gamma參數(shù)給出了核函數(shù)中除線性內(nèi)積函數(shù)以外的所有函數(shù)的參數(shù),默認(rèn)值為l。coef0參數(shù)是指核函數(shù)中多項式內(nèi)積函數(shù)與sigmoid內(nèi)積函數(shù)中的參數(shù),默認(rèn)值為0。
另外,參數(shù)cost就是軟間隔模型中的離群點權(quán)重。最后,參數(shù)nu是用于nu-regression、nu-classification和one-classification類型中的參數(shù)。
一個經(jīng)驗性的結(jié)論是,在利用svm()函數(shù)建立支持向量機(jī)模型時,使用標(biāo)準(zhǔn)化后的數(shù)據(jù)建立的模型效果更好。

colnames(Data)<-c("class","id","R1","G1","B1","R2","G2","B2","R3","G3","B3")

head(Data)

## class id R1 G1 B1 R2 G2

## 1 1 1 0.5828229 0.5437737 0.2528287 0.014192030 0.016143875

## 2 1 10 0.6416595 0.5706572 0.2137280 0.015438840 0.011177918

## 3 1 11 0.6036844 0.5767189 0.2822538 0.008658572 0.007074807

## 4 1 12 0.5897057 0.5937430 0.2522425 0.007908293 0.005940868

## 5 1 13 0.5910962 0.5920930 0.2535949 0.007448469 0.006494667

## 6 1 14 0.5886801 0.5696339 0.3189053 0.007527690 0.005046087

## B2 R3 G3 B3

## 1 0.041075252 -0.012643137 -0.016090364 -0.041536239

## 2 0.013707795 0.009727136 -0.003723814 -0.003779448

## 3 0.012203640 -0.004694985 -0.002570890 -0.009450531

## 4 0.010568364 0.003303400 -0.003416659 -0.005273416

## 5 0.012151602 0.000496116 -0.002235644 -0.005095575

## 6 0.008386259 -0.003529253 0.001746734 -0.005790924

#數(shù)據(jù)分割

set.seed(1234)#設(shè)置隨機(jī)種子

#定義序列ind,隨機(jī)抽取1和2,1的個數(shù)占80%,2的個數(shù)占20%

ind <- sample(2, nrow(Data), replace=TRUE, prob=c(0.8, 0.2))

traindata <-Data[ind==1,]#訓(xùn)練數(shù)據(jù)

testdata <- Data[ind==2,]#測試數(shù)據(jù)

#將class列轉(zhuǎn)換為factor類型

traindata<-transform(traindata,class=as.factor(class))

testdata<-transform(testdata,class=as.factor(class))

##支持向量機(jī)分類模型構(gòu)建

library(e1071)#加載e1071包

#利用svm建立支持向量機(jī)分類模型

svm.model<-svm(class~., traindata[,-2])

summary(svm.model)

##

## Call:

## svm(formula = class ~ ., data = traindata[, -2])

##

##

## Parameters:

## SVM-Type: C-classification

## SVM-Kernel: radial

## cost: 1

## gamma: 0.1111111

##

## Number of Support Vectors: 119

##

## ( 31 26 41 16 5 )

##

##

## Number of Classes: 5

##

## Levels:

## 1 2 3 4 5

#通過summary函數(shù)可以得到關(guān)于模型的相關(guān)信息。其中,SVM-Type項目說明本模型的類別為C分類器模型;SVM-Kernel項目說明本模型所使用的核函數(shù)為高斯內(nèi)積函數(shù)且核函數(shù)中參數(shù)gamma的取值為0.11;cost項目說明本模型確定的約束違反成本為l。而且我們還可以看到,模型找到了119個支持向量:第一類包含有31個支持向量,第二類包含有26個支持向量,第三類包含41個支持向量,第四類包含有16個支持向量,第三類包含5個支持向量。最后一行說明模型中的三個類別分別為1、2、3、4、5.

#建立混淆矩陣

#訓(xùn)練集

confusion.train.svm=table(traindata$class,predict(svm.model,traindata,type="class"))

accuracy.train.svm=sum(diag(confusion.train.svm))/sum(confusion.train.svm)

confusion.train.svm

##

## 1 2 3 4 5

## 1 41 0 2 0 0

## 2 0 37 0 0 0

## 3 1 0 61 0 0

## 4 0 0 2 15 0

## 5 1 0 0 0 4

accuracy.train.svm
## [1] 0.9634146

#測試集

confusion.test.svm=table(testdata$class,predict(svm.model,testdata,type="class"))

accuracy.test.svm=sum(diag(confusion.test.svm))/sum(confusion.test.svm)

confusion.test.svm

##

## 1 2 3 4 5

## 1 8 0 0 0 0

## 2 0 7 0 0 0

## 3 0 0 16 0 0

## 4 0 1 2 4 0

## 5 0 0 0 0 1

accuracy.test.svm
## [1] 0.9230769

#隨機(jī)森林

library(randomForest)

## randomForest 4.6-12
## Type rfNews() to see new features/changes/bug fixes.

randomForest.model<-randomForest(class~., traindata[,-2])

summary(randomForest.model)

## Length Class Mode

## call 3 -none- call

## type 1 -none- character

## predicted 164 factor numeric

## err.rate 3000 -none- numeric

## confusion 30 -none- numeric

## votes 820 matrix numeric

## oob.times 164 -none- numeric

## classes 5 -none- character

## importance 9 -none- numeric

## importanceSD 0 -none- NULL

## localImportance 0 -none- NULL

## proximity 0 -none- NULL

## ntree 1 -none- numeric

## mtry 1 -none- numeric

## forest 14 -none- list

## y 164 factor numeric

## test 0 -none- NULL

## inbag 0 -none- NULL

## terms 3 terms call

randomForest.model

##

## Call:

## randomForest(formula = class ~ ., data = traindata[, -2])

## Type of random forest: classification

## Number of trees: 500

## No. of variables tried at each split: 3

##

## OOB estimate of error rate: 9.15%

## Confusion matrix:

## 1 2 3 4 5 class.error

## 1 36 3 4 0 0 0.16279070

## 2 1 35 1 0 0 0.05405405

## 3 2 0 60 0 0 0.03225806

## 4 0 0 1 16 0 0.05882353

## 5 1 0 0 2 2 0.60000000

#訓(xùn)練集

confusion.train.randomForest=table(traindata$class,predict(randomForest.model,traindata,type="class"))

accuracy.train.randomForest=sum(diag(confusion.train.randomForest))/sum(confusion.train.randomForest)

confusion.train.randomForest

##

## 1 2 3 4 5

## 1 43 0 0 0 0

## 2 0 37 0 0 0

## 3 0 0 62 0 0

## 4 0 0 0 17 0

## 5 0 0 0 0 5

accuracy.train.randomForest
## [1] 1

#測試集

confusion.test.randomForest=table(testdata$class,predict(randomForest.model,testdata,type="class"))

accuracy.test.randomForest=sum(diag(confusion.test.randomForest))/sum(confusion.test.randomForest)

confusion.test.randomForest

##

## 1 2 3 4 5

## 1 7 0 1 0 0

## 2 0 7 0 0 0

## 3 1 0 15 0 0

## 4 0 0 2 5 0

## 5 0 0 0 0 1

accuracy.test.randomForest
## [1] 0.8974359

#神經(jīng)網(wǎng)絡(luò)

library(nnet)

nnet.model<-nnet(class~., traindata[,-2],size=30,decay=.001)

## # weights: 455

## initial value 318.920319

## iter 10 value 176.714302

## iter 20 value 57.798855

## iter 30 value 42.657486

## iter 40 value 27.296733

## iter 50 value 20.803959

## iter 60 value 18.519644

## iter 70 value 16.706718

## iter 80 value 15.700517

## iter 90 value 15.200025

## iter 100 value 14.797823

## final value 14.797823

## stopped after 100 iterations

summary(nnet.model)

## a 9-30-5 network with 455 weights

## options were - softmax modelling decay=0.001

## b->h1 i1->h1 i2->h1 i3->h1 i4->h1 i5->h1 i6->h1 i7->h1 i8->h1 i9->h1

## -2.75 -1.05 -1.31 -0.04 0.00 0.00 -0.03 0.06 0.00 0.11

## b->h2 i1->h2 i2->h2 i3->h2 i4->h2 i5->h2 i6->h2 i7->h2 i8->h2 i9->h2

## 1.55 -2.29 -0.37 -0.76 1.02 1.46 1.91 -1.90 -2.21 -2.26

## b->h3 i1->h3 i2->h3 i3->h3 i4->h3 i5->h3 i6->h3 i7->h3 i8->h3 i9->h3

## 3.06 2.93 2.01 -17.11 1.57 0.56 0.62 -0.89 0.67 3.71

## b->h4 i1->h4 i2->h4 i3->h4 i4->h4 i5->h4 i6->h4 i7->h4 i8->h4 i9->h4

## 13.76 -20.60 -2.70 -13.91 0.05 0.26 1.69 -0.41 -0.87 -1.86

## b->h5 i1->h5 i2->h5 i3->h5 i4->h5 i5->h5 i6->h5 i7->h5 i8->h5 i9->h5

## 8.63 -7.74 -8.29 -0.52 -5.14 -4.83 -5.11 6.94 2.07 0.17

## b->h6 i1->h6 i2->h6 i3->h6 i4->h6 i5->h6 i6->h6 i7->h6 i8->h6 i9->h6

## 2.16 -7.64 0.96 4.96 1.28 2.07 2.49 -2.65 -1.87 -3.63

## b->h7 i1->h7 i2->h7 i3->h7 i4->h7 i5->h7 i6->h7 i7->h7 i8->h7 i9->h7

## 7.74 -7.29 -6.89 -4.14 -1.00 -0.61 0.63 1.61 -1.54 -5.57

## b->h8 i1->h8 i2->h8 i3->h8 i4->h8 i5->h8 i6->h8 i7->h8 i8->h8 i9->h8

## -6.20 6.18 5.23 -0.35 4.25 3.92 4.70 -5.18 -2.24 -3.47

## b->h9 i1->h9 i2->h9 i3->h9 i4->h9 i5->h9 i6->h9 i7->h9 i8->h9 i9->h9

## 7.43 -6.77 -11.18 7.93 -5.95 -5.05 -4.73 7.39 1.18 -4.61

## b->h10 i1->h10 i2->h10 i3->h10 i4->h10 i5->h10 i6->h10 i7->h10 i8->h10

## 2.12 0.33 0.54 -0.99 0.11 0.04 0.11 -0.03 -0.09

## i9->h10

## 0.06

## b->h11 i1->h11 i2->h11 i3->h11 i4->h11 i5->h11 i6->h11 i7->h11 i8->h11

## -2.55 0.01 -0.82 -0.21 -0.22 -0.18 -0.32 0.06 0.12

## i9->h11

## 0.54

## b->h12 i1->h12 i2->h12 i3->h12 i4->h12 i5->h12 i6->h12 i7->h12 i8->h12

## -18.76 15.10 9.42 20.70 1.89 0.88 2.24 1.13 3.40

## i9->h12

## -11.18

## b->h13 i1->h13 i2->h13 i3->h13 i4->h13 i5->h13 i6->h13 i7->h13 i8->h13

## 2.17 -11.66 0.77 13.47 -2.00 -0.48 -1.18 -0.16 -0.14

## i9->h13

## -0.44

## b->h14 i1->h14 i2->h14 i3->h14 i4->h14 i5->h14 i6->h14 i7->h14 i8->h14

## 4.90 -14.11 4.32 -7.64 1.13 1.22 1.62 -2.77 -0.60

## i9->h14

## 1.82

## b->h15 i1->h15 i2->h15 i3->h15 i4->h15 i5->h15 i6->h15 i7->h15 i8->h15

## -2.00 -0.21 -1.04 -0.65 -0.22 -0.17 -0.26 0.19 0.06

## i9->h15

## 0.34

## b->h16 i1->h16 i2->h16 i3->h16 i4->h16 i5->h16 i6->h16 i7->h16 i8->h16

## 0.55 -0.72 1.13 1.70 0.21 0.33 0.16 -0.40 -0.18

## i9->h16

## 0.23

## b->h17 i1->h17 i2->h17 i3->h17 i4->h17 i5->h17 i6->h17 i7->h17 i8->h17

## 1.95 -1.02 0.93 -0.71 0.08 0.13 0.02 -0.18 -0.07

## i9->h17

## -0.02

## b->h18 i1->h18 i2->h18 i3->h18 i4->h18 i5->h18 i6->h18 i7->h18 i8->h18

## -1.94 0.39 -0.65 -0.33 -0.43 -0.58 -0.58 0.56 0.36

## i9->h18

## 0.89

## b->h19 i1->h19 i2->h19 i3->h19 i4->h19 i5->h19 i6->h19 i7->h19 i8->h19

## -2.89 -0.62 -1.17 -0.62 -0.03 -0.05 -0.15 0.05 0.05

## i9->h19

## 0.25

## b->h20 i1->h20 i2->h20 i3->h20 i4->h20 i5->h20 i6->h20 i7->h20 i8->h20

## 2.69 0.93 1.39 0.74 0.30 0.32 0.45 -0.33 -0.34

## i9->h20

## -0.31

## b->h21 i1->h21 i2->h21 i3->h21 i4->h21 i5->h21 i6->h21 i7->h21 i8->h21

## -2.97 -0.45 -1.26 0.46 -0.13 -0.19 -0.35 0.24 0.15

## i9->h21

## 0.53

## b->h22 i1->h22 i2->h22 i3->h22 i4->h22 i5->h22 i6->h22 i7->h22 i8->h22

## -2.02 -0.48 -1.09 -0.70 -0.07 -0.14 -0.26 0.21 0.04

## i9->h22

## 0.34

## b->h23 i1->h23 i2->h23 i3->h23 i4->h23 i5->h23 i6->h23 i7->h23 i8->h23

## 11.00 -9.85 -5.03 -7.26 -5.00 -5.03 -6.66 6.29 3.49

## i9->h23

## 9.93

## b->h24 i1->h24 i2->h24 i3->h24 i4->h24 i5->h24 i6->h24 i7->h24 i8->h24

## 0.09 0.10 1.19 0.87 0.15 0.18 0.02 -0.27 -0.03

## i9->h24

## 0.35

## b->h25 i1->h25 i2->h25 i3->h25 i4->h25 i5->h25 i6->h25 i7->h25 i8->h25

## -1.65 4.19 -0.24 -1.84 -1.58 -2.09 -3.09 2.29 2.50

## i9->h25

## 6.02

## b->h26 i1->h26 i2->h26 i3->h26 i4->h26 i5->h26 i6->h26 i7->h26 i8->h26

## 1.60 2.12 0.63 -9.24 3.25 3.09 3.24 -3.76 -2.22

## i9->h26

## -0.40

## b->h27 i1->h27 i2->h27 i3->h27 i4->h27 i5->h27 i6->h27 i7->h27 i8->h27

## -1.77 1.13 -1.39 -1.13 -0.43 -0.47 -0.68 0.41 0.18

## i9->h27

## 1.08

## b->h28 i1->h28 i2->h28 i3->h28 i4->h28 i5->h28 i6->h28 i7->h28 i8->h28

## -0.24 4.65 0.83 -9.53 2.28 2.06 2.00 -2.98 -2.04

## i9->h28

## 1.40

## b->h29 i1->h29 i2->h29 i3->h29 i4->h29 i5->h29 i6->h29 i7->h29 i8->h29

## -2.92 -0.57 -1.21 0.07 -0.18 -0.08 -0.14 0.13 0.06

## i9->h29

## 0.25

## b->h30 i1->h30 i2->h30 i3->h30 i4->h30 i5->h30 i6->h30 i7->h30 i8->h30

## -2.17 2.89 2.08 -0.17 -0.80 -1.19 -2.03 1.25 2.02

## i9->h30

## 5.09

## b->o1 h1->o1 h2->o1 h3->o1 h4->o1 h5->o1 h6->o1 h7->o1 h8->o1

## -1.61 -0.73 -1.36 11.20 -5.48 -8.67 -3.12 -5.21 5.32

## h9->o1 h10->o1 h11->o1 h12->o1 h13->o1 h14->o1 h15->o1 h16->o1 h17->o1

## -12.47 -0.23 -0.50 15.65 -11.70 -3.57 -1.02 -1.60 -0.80

## h18->o1 h19->o1 h20->o1 h21->o1 h22->o1 h23->o1 h24->o1 h25->o1 h26->o1

## 0.30 -0.47 1.03 -2.01 -0.76 -4.20 -0.88 3.70 3.09

## h27->o1 h28->o1 h29->o1 h30->o1

## -0.48 3.23 -0.84 2.52

## b->o2 h1->o2 h2->o2 h3->o2 h4->o2 h5->o2 h6->o2 h7->o2 h8->o2

## 4.22 -0.06 -2.83 -10.27 -4.22 5.12 1.71 -2.68 -4.57

## h9->o2 h10->o2 h11->o2 h12->o2 h13->o2 h14->o2 h15->o2 h16->o2 h17->o2

## 8.36 -1.34 -0.73 5.57 13.82 -2.43 -0.22 1.78 0.33

## h18->o2 h19->o2 h20->o2 h21->o2 h22->o2 h23->o2 h24->o2 h25->o2 h26->o2

## -0.10 -0.19 -0.19 0.28 -0.18 6.00 1.17 1.99 -10.20

## h27->o2 h28->o2 h29->o2 h30->o2

## -0.72 -9.77 -0.24 0.65

## b->o3 h1->o3 h2->o3 h3->o3 h4->o3 h5->o3 h6->o3 h7->o3 h8->o3

## -1.54 4.15 -0.36 5.06 -15.39 -0.59 -4.92 -3.20 0.79

## h9->o3 h10->o3 h11->o3 h12->o3 h13->o3 h14->o3 h15->o3 h16->o3 h17->o3

## -6.78 2.10 2.95 -16.51 -4.10 -4.52 2.53 0.26 0.79

## h18->o3 h19->o3 h20->o3 h21->o3 h22->o3 h23->o3 h24->o3 h25->o3 h26->o3

## 1.46 3.31 -1.69 5.18 2.72 7.33 1.37 3.03 5.39

## h27->o3 h28->o3 h29->o3 h30->o3

## 1.82 8.05 5.08 3.49

## b->o4 h1->o4 h2->o4 h3->o4 h4->o4 h5->o4 h6->o4 h7->o4 h8->o4

## -0.22 0.95 -0.08 -2.06 5.33 11.89 -2.25 8.77 -5.54

## h9->o4 h10->o4 h11->o4 h12->o4 h13->o4 h14->o4 h15->o4 h16->o4 h17->o4

## 13.74 -2.61 0.54 -9.44 -4.01 -4.70 1.03 -2.56 -1.48

## h18->o4 h19->o4 h20->o4 h21->o4 h22->o4 h23->o4 h24->o4 h25->o4 h26->o4

## 0.51 0.74 -2.16 1.63 1.09 5.32 -2.31 -0.28 -0.19

## h27->o4 h28->o4 h29->o4 h30->o4

## 1.54 -0.34 1.04 -2.99

## b->o5 h1->o5 h2->o5 h3->o5 h4->o5 h5->o5 h6->o5 h7->o5 h8->o5

## -0.96 -4.20 4.76 -4.01 19.82 -7.68 8.59 2.49 3.97

## h9->o5 h10->o5 h11->o5 h12->o5 h13->o5 h14->o5 h15->o5 h16->o5 h17->o5

## -2.97 2.07 -2.33 4.71 6.04 15.20 -2.48 2.17 1.26

## h18->o5 h19->o5 h20->o5 h21->o5 h22->o5 h23->o5 h24->o5 h25->o5 h26->o5

## -1.96 -3.36 2.99 -5.00 -2.73 -14.51 0.60 -8.41 1.90

## h27->o5 h28->o5 h29->o5 h30->o5

## -2.11 -1.15 -5.09 -3.74

nnet.model

## a 9-30-5 network with 455 weights

## inputs: R1 G1 B1 R2 G2 B2 R3 G3 B3

## output(s): class

## options were - softmax modelling decay=0.001

#訓(xùn)練集

confusion.train.nnet=table(traindata$class,predict(nnet.model,traindata,type="class"))

accuracy.train.nnet=sum(diag(confusion.train.nnet))/sum(confusion.train.nnet)

confusion.train.nnet

##

## 1 2 3 4 5

## 1 43 0 0 0 0

## 2 0 37 0 0 0

## 3 0 0 62 0 0

## 4 0 0 0 17 0

## 5 0 0 0 0 5

accuracy.train.nnet
## [1] 1

#測試集

confusion.test.nnet=table(testdata$class,predict(nnet.model,testdata,type="class"))

accuracy.test.nnet=sum(diag(confusion.test.nnet))/sum(confusion.test.nnet)

confusion.test.nnet

##

## 1 2 3 4 5

## 1 8 0 0 0 0

## 2 0 7 0 0 0

## 3 0 0 16 0 0

## 4 0 0 1 6 0

## 5 0 0 0 0 1

accuracy.test.nnet
## [1] 0.974359

#對比支持向量機(jī)、隨機(jī)森林、人工神經(jīng)網(wǎng)絡(luò)算法的準(zhǔn)確率

accuracy.svm <-c(accuracy.train.svm,accuracy.test.svm)

accuracy.randomForest<-c(accuracy.train.randomForest,accuracy.test.randomForest)

accuracy.nnet <-c(accuracy.train.nnet,accuracy.test.nnet)

accuracy.data <-data.frame(accuracy.svm,accuracy.randomForest,accuracy.nnet)

accuracy.data

## accuracy.svm accuracy.randomForest accuracy.nnet

## 1 0.9634146 1.0000000 1.000000

## 2 0.9230769 0.8974359 0.974359

第一行是訓(xùn)練集準(zhǔn)確率,第二行是測試集準(zhǔn)確率。

1.支持向量機(jī)雖然在訓(xùn)練集擬合度不如隨機(jī)森林和神經(jīng)網(wǎng)絡(luò),但是測試集準(zhǔn)確率較高;

2.隨機(jī)森林明顯過擬合;

3.對比發(fā)現(xiàn)神經(jīng)網(wǎng)絡(luò)不管訓(xùn)練集還是測試集效果都最好。

該對比只是簡單的對比,不能直接說明哪種算法最好。

原因:

1.數(shù)據(jù)樣本過少;

2.實際使用算法中還要考慮到算法運行的時間,當(dāng)面對海量數(shù)據(jù)時,準(zhǔn)確復(fù)雜的算法往往運行過慢。

3.算法得出的模型“好壞”,不僅僅只看準(zhǔn)確率,還要看其他指標(biāo)比如:recall、percision、F1-score等。比如地震預(yù)測更看重recall指標(biāo)

4.實際中還是要結(jié)合具體情況,選擇合適的算法。

以后有時間再將各種算法進(jìn)行復(fù)雜的對比。

總結(jié)

以上是生活随笔為你收集整理的支持向量机SVM算法原理及应用(R)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。