1. 加载R包和数据
# 安装R包, 是否update统一选择不更新n
BiocManager::install("caret")
BiocManager::install("randomForest")
BiocManager::install("gbm")
BiocManager::install("kernlab")
BiocManager::install("glmnet")
library(caret)
library(e1071)
library(rpart)
library(randomForest)
library(gbm)
library(kernlab)
library(nnet)
library(glmnet)
# 鸢尾花数据集 iris
data(iris)
# 划分数据集,设置随机种子以保证结果的可重复性
set.seed(1234)
# 80%数据为训练集,剩余20%为测试集
trainIndex <- createDataPartition(iris$Species, p =.8, list = FALSE, times = 1)
trainData <- iris[trainIndex,]
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 2 4.9 3.0 1.4 0.2 setosa
# 3