R语言(一)SVM & LDA

The R package “e1071” has the implementation of SVM with a number of kernel choices. Try to classify Ionosphere dataset from “mlbench” package with:

library(e1071)
library(mlbench)
data(Ionosphere)

(1) Linear kernel, polynomial kernel with different degrees, and radial basis kernel.

linear.model <- svm(x=Ionosphere[,-35],y=Ionosphere[,35],kernel='linear',type='C-classification',scale=FALSE)
poly3.model <- svm(x=Ionosphere[,-35],y=Ionosphere[,35],kernel='polynomial',degree=3,type='C-classification',scale=FALSE)
poly6.model <- svm(x=Ionosphere[,-35],y=Ionosphere[,35],kernel='polynomial',degree=6,type='C-classification',scale=FALSE)
radial.model <- svm(x=Ionosphere[,-35],y=Ionosphere[,35],kernel='radial',type='C-classification',scale=FALSE)

(2) Benchmark your classification accuracy using 10-fold cross-validation.

library(caret)
set.seed(1)
fold <- createFolds(Ionosphere$Class,k=10)
linearTrue <- c()
poly3True <- c()
poly6True <- c()
radialTrue <- c()
for(i in 1:length(fold)){
  truth <- Ionosphere$Class[fold[[i]]]
  linearPreds <- predict(linear.model,newdata = Ionosphere[fold[[i]],-35])
  poly3Preds <- predict(poly3.model,newdata = Ionosphere[fold[[i]],-35])
  poly6Preds <- predict(poly6.model,newdata = Ionosphere[fold[[i]],-35])
  radialPreds <- predict(radial.model,newdata = Ionosphere[fold[[i]],-35])
  linearTrue <- c(linearTrue,sum(linearPreds==truth))
  poly3True <- c(poly3True,sum(poly3Preds==truth))
  poly6True <- c(poly6True,sum(poly6Preds==truth))
  radialTrue <- c(radialTrue,sum(radialPreds==truth))
}
cat(c("Linear kernel accuracy:",sum(linearTrue)/nrow(Ionosphere),"\n"))
## Linear kernel accuracy: 0.923076923076923
cat(c("Polynomial kernel with degree 3 accuracy:",sum(poly3True)/nrow(Ionosphere),"\n"))
## Polynomial kernel with degree 3 accuracy: 0.689458689458689
cat(c("Polynomial kernel with degree 6 accuracy:",sum(poly6True)/nrow(Ionosphere),"\n"))
## Polynomial kernel with degree 6 accuracy: 0.641025641025641
cat(c("Radial kernel accuracy:",sum(radialTrue)/nrow(Ionosphere),"\n"))
## Radial kernel accuracy: 0.945868945868946

(3) Repeat the above classification using LDA with 10-fold cross-validation.

library(MASS)
ionosphere <- Ionosphere[,-2] #delete constant column
lda.model <- lda(Class~.,ionosphere) 
ldaTrue <- c()
for(i in 1:length(fold)){
  truth <- ionosphere$Class[fold[[i]]]
  ldaPreds <- predict(lda.model,ionosphere[fold[[i]],-34])$posterior[,'good'] 
  #cat(ldaPreds)
  lda.decision <- ifelse(ldaPreds > 0.5,'good','bad')
  ldaTrue <- c(ldaTrue,sum(lda.decision==truth))
}
cat(c("LDA accuracy:",sum(ldaTrue)/nrow(ionosphere)))
## LDA accuracy: 0.9002849002849

(4) Comment on the linear separability of the data based on the classification result using SVM with different kernels and LDA.

The linear separability of the data is high. Because accuracy of both linear kernel SVM and LDA are high, while accuracy of polynomial kernel SVM with degree 3 and 6 are low.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我一直以来都是一个特别不能坚持的人。对于一件事情我最多最多坚持两个月,只要到了这个时间段,我的思想就会开始松懈,对...
    最可爱的靖靖阅读 339评论 0 0
  • 当初喜欢他的什么?喜欢他的幽默风趣,喜欢他的善良大方,喜欢他对父母仁义孝顺。不知为什么,当走进他之后,发现,原...
    王恬影阅读 272评论 2 0
  • 真心实意交流 是互信的桥梁 团结友善做事 是共赢的道路
    梁文成阅读 412评论 0 3
  • 初始化控件在viewDidLoad中去做
    要加油啊小和尚阅读 588评论 0 50