2018-10-28
閱讀量:
1027
支持向量機(jī)的R實(shí)現(xiàn)
#(1)支持向量機(jī)(SVM):
library(kernlab)
irismodel <- ksvm(Species ~ ., data = iris,? ?? ?? ?? ???
? ?? ?? ?? ?? ?? ?? ?type = "C-bsvc", kernel = "rbfdot",? ?? ?? ?? ?? ?? ???
? ?? ?? ?? ?? ?? ?? ?kpar = list(sigma = 0.1), C = 10,? ?? ?? ?? ?? ?? ???
? ?? ?? ?? ?? ?? ?? ?prob.model = TRUE)
irismodel
predict(irismodel, iris[c(3, 10, 56, 68, 107, 120), -5], type = "probabilities")
#Ksvm支持自定義核函數(shù)。如
k <- function(x, y) { (sum(x * y) + 1) * exp(0.001 * sum((x - y)^2)) }
class(k) <- "kernel"
data("promotergene")
gene <- ksvm(Class ~ ., data = promotergene, kernel = k, C = 10, cross = 5)#訓(xùn)練
gene
#對(duì)于二分類問(wèn)題,可以對(duì)結(jié)果用plot()進(jìn)行可視化。例子如下
x <- rbind(matrix(rnorm(120), , 2), matrix(rnorm(120, mean = 3), , 2))
y <- matrix(c(rep(1, 60), rep(-1, 60)))
svp <- ksvm(x, y, type = "C-svc", kernel = "rbfdot", kpar = list(sigma = 2))
plot(svp)
library(e1071)
set.seed(1234)
ind<-sample(2,nrow(iris),replace=TRUE,prob=c(0.7,0.3)) #70%為訓(xùn)練集 30%為測(cè)試集
train<-iris[ind==1,]
test<-iris[ind==2,]
svm<-svm(train[,1:4],train[,5],type="C-classification",
? ?? ?? ?cost=10,kernel="radial",probability=TRUE,scale=FALSE)
pred<-predict(svm,test[,1:4],decision.values=TRUE)
table(pred,test[,5])
library(e1071)
model <- svm(Species ~ ., data = iris,? ?? ?? ?? ?
? ?? ?? ?? ?? ?method = "C-classification", kernel = "radial",? ?? ?? ?? ???
? ?? ?? ?? ?? ?cost = 10, gamma = 0.1)
summary(model)
plot(model, iris, Petal.Width ~
? ?? ? Petal.Length, slice = list(Sepal.Width = 3,? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? Sepal.Length = 4))
pre=predict(model, iris,type='class')
table(pre,iris$Species)
library("klaR")
data("B3")
Bmod <- svmlight(PHASEN ~ ., data = B3,? ?? ?? ?? ?? ?? ?
? ?? ?? ?? ?? ?? ? svm.options = "-c 10 -t 2 -g 0.1 -v 0")
predict(Bmod, B3[c(4, 9, 30, 60, 80, 120),? ?? ?? ?? ???
? ?? ?? ?? ?? ?? ? -1])
#(2)支持向量回歸(SVR):
library(DMwR)
library(nnet)
data(algae)
algae <- algae[-manyNAs(algae), ]
clean.algae <- knnImputation(algae[,1:12],k=10)
norm.data <- scale(clean.algae[,4:12]) #數(shù)據(jù)標(biāo)準(zhǔn)化
library(e1071)
model.svm <- svm(a1~., norm.data)
preds <- predict(model.svm, norm.data)
plot(preds~ scale(clean.algae$a1))
library(rminer)
set.seed(1234)
svr<-fit(a1~., norm.data, model="svm")
#利用模型進(jìn)行預(yù)測(cè)
norm.preds <- predict(svr, norm.data)
#繪制預(yù)測(cè)值與真實(shí)值之間的散點(diǎn)圖
plot(norm.preds~ scale(clean.algae$a1))
#計(jì)算相對(duì)誤差
(nmse2 <- mean((norm.preds-scale(clean.algae$a1))^2)/
?mean((mean(scale(clean.algaea1))-scale(clean.algaea1))^2))






評(píng)論(0)


暫無(wú)數(shù)據(jù)
CDA考試動(dòng)態(tài)
CDA報(bào)考指南
推薦帖子
0條評(píng)論
0條評(píng)論
0條評(píng)論