机器学习机器学习与网络

机器学习笔记7-R语言实现多种分类算法

2021-10-14  本文已影响0人  江湾青年
############## 数据准备 ############
library(neuralnet)
library(caret)

data("iris")
head(iris)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa

# 划分训练集和测试集
indexes <- sample(1:150,100)
train <- iris[indexes, ]
test <- iris[-indexes, ] 
xtest <- test[, -5]    # 测试集去掉类标
ytest <- test[, 5]     # 测试集的类标


############## NeuralNet ############
library(neuralnet)
library(caret)

nnet <- neuralnet(Species~., train, hidden = c(4,3), linear.output = FALSE)
plot(nnet)
# 对xtest进行测试
ypred <- neuralnet::compute(nnet, xtest)
y_pre <- levels(iris$Species)[max.col(ypred$net.result)]
length(which(y_pre <-<- ytest))/length(ytest)
# 0.98
cm <- confusionMatrix(as.factor(ytest), as.factor(y_pre))
print(cm)


############## RandomForest ############
library(randomForest)

RF <- randomForest(Species ~ ., data = train, importance=TRUE, proximity=TRUE,ntree=500,mtry=2)
# 查看特征的重要性
varImpPlot(RF, main = "variable importance")

#对测试集进行预测
pred <- predict(RF,newdata = xtest)
length(which((pred == ytest) == T))/length(ytest)
# 0.94


############## NaiveBayes ############
library(klaR)
NB <- NaiveBayes(Species ~ ., data = train,usekernel = FALSE, fL = 1)
# usekernel:是否使用核密度估计器估计密度函数。
# fL:是否进行拉普拉斯修正,默认情况下不进行修正。数据量较小时可以设置为1,进行拉普拉斯修正。
knb_predict <- predict(NB,newdata = xtest)
#生成实际与预测交叉表和预测精度
table(ytest ,predict=knb_predict$class)
length(which((knb_predict$class == ytest) == T))/length(ytest)
# 0.96


############## KNN ############
library(class)
knn_predict <- knn(train[,-5], xtest, cl = train[,5], k = 5)
length(which((knn_predict == ytest) == T))/length(ytest)
# 0.98




############## SVM ############
library(e1071)
svm.model<- svm(Species ~ ., data = train)
svm.pred <- predict(svm.model,xtest)
length(which((svm.pred == ytest) == T))/length(ytest)
# 0.94



########### multinomial logistic regression #############
library(nnet)
mlr.model <- nnet::multinom(Species ~., data = train)
mlr.out <- predict(mlr.model,xtest)
length(which((mlr.out == ytest) == T))/length(ytest)
# 0.98





上一篇下一篇

猜你喜欢

热点阅读