常见的五种分类算法

k-近邻、决策树、朴素贝叶斯、神经网络、支持向量机分类算法(以iris为例)

所需安装包:

ibrary(class)##knn
library(C50)##决策树
library(e1071)##朴素贝叶斯and支持向量机
library(neuralnet)##神经网络

构造算法精度函数

##knn_acc function
knn_acc <- function(train.data,test.data,k){
  pre_knn = knn(train.data[,1:4],test.data[,1:4],train.data[,5],k)
  ##complete acc
  acc_knn = sum(diag(table(pre_knn,test.data[,5])))/dim(test.data)[1]
}
##tree_acc function
tree_acc <- function(train.data,test.data){
  tree = C5.0(train.data$Species~.,train.data)
  pre_tree = predict(tree,newdata = test.data)
  acc_tree = sum(diag(table(pre_tree,test.data[,5])))/dim(test.data)[1]
}
##naivebayes_acc function
naivebayes_acc <- function(train.data,test.data){
  naivebayes1 = naiveBayes(train.data$Species~.,train.data)
  pre_naivebayes = predict(naivebayes1,newdata = test.data)
  acc_naivebayes = sum(diag(table(pre_naivebayes,test.data[,5])))/dim(test.data)[1]
}
##neuralnet_acc function
neuralnet_acc <- function(train.data,test.data){
  neuralnet1 = neuralnet(train.data$Species~.,train.data,hidden = 2)
  pre_neuralnet = compute(neuralnet1,test.data[-5])$net.result
  colnames(pre_neuralnet) = levels(train.data[,5])
  idx = apply(pre_neuralnet,1,which.max)
  class = colnames(pre_neuralnet)[idx]
  acc_neuralnet = sum(diag(table(class,test.data[,5])))/dim(test.data)[1]
}
##svm_acc function
svm_acc <- function(train.data,test.data){
  svm1 = svm(train.data$Species~.,train.data, kernel = "linear",gamma = 0.1,cost = 10)
  pre_svm = predict(svm1,test.data)
  acc_svm = sum(diag(table(pre_svm,test.data[,5])))/dim(test.data)[1]
}

利用十折交叉检验,代入iris数据对五种算法精度进行对比

acc_all = c()
for(i in 1:10){
  test.idx=seq(i,150,by=10)
  train.idx = setdiff(1:150,test.idx)
  test.data = iris[test.idx,]
  train.data = iris[train.idx,]
  ######predict Species by knn function
  ACC_knn = knn_acc(train.data,test.data,k=4)
  ######predict Species by tree function
  ACC_tree = tree_acc(train.data,test.data)
  ######predict Species by naivebayes function
  ACC_naivebayes = naivebayes_acc(train.data,test.data)
  ######predict Species by neuralnet function
  ACC_neuralnet = neuralnet_acc(train.data,test.data)
  ######predict Species by svm function
  ACC_svm = svm_acc(train.data,test.data)
  ##save acc_all
  acc_all = rbind(acc_all,c(ACC_knn,ACC_tree,ACC_naivebayes,ACC_neuralnet,ACC_svm))
  colnames(acc_all) = c("knn","tree","naivebayes","neuralnet","svm")
}
acc = apply(acc_all,2,mean)

输出结果:

pdf("E:/Rwork/classify.pdf",width = 2.5 ,height = 2.5)
barplot(acc,ylim = c(0,1),col = brewer.pal(5,"Set3"),ylab="Classification accuracy")
dev.off()

常见的五种分类算法_第1张图片

你可能感兴趣的:(R,分类算法,支持向量机,svm)