如何使用 R 中的 Caret 包进行训练、验证和测试?
How to do train, validation and test using Caret package in R?
我是使用 Caret 包进行训练、验证和测试的初学者。我在网上进行了研究,发现它有些令人困惑(似乎大多数显示训练和测试都没有验证),你能展示一个代码示例来说明它是如何工作的吗?
train
允许您进行验证等等。您可以为 trControl
参数提供一个 trainControl
函数,允许您指定训练过程的细节。默认情况下 train
已经将您传入的数据的 75% 拆分为训练,25% 用于验证,您也可以在 trainControl
.
中更改此设置
我建议您查看 train
和 trainControl
文档,here and here 以了解有关您可以在训练过程中指定的详细信息的更多信息。
下面是一个使用 5 折交叉验证训练随机森林并使用 Caret 和 train
函数对数据进行标准化以更好地举例说明的简单示例。
(注意:我添加了一些不必要的东西,例如 verboseIter = TRUE
和 classProbs = TRUE
只是为了向您展示一些使用插入符号获得的功能)
library(caret)
library(datasets)
# Loading the iris dataset
data(iris)
# Specifying an 80-20 train-test split
train_idx = createDataPartition(iris$Species, p = .8, list = F)
# Creating the training and testing sets
train = iris[train_idx, ]
test = iris[-train_idx, ]
# Declaring the trainControl function
train_ctrl = trainControl(
method = "cv", #Specifying Cross validation
number = 5, # Specifying 5-fold
verboseIter = TRUE, # So that each iteration you get an update of the progress
classProbs = TRUE # So that you can obtain the probabilities for each example
)
rf_model = train(
Species ~., # Specifying the response variable and the feature variables
method = "rf", # Specifying the model to use
data = train,
trControl = train_ctrl,
preProcess = c("center", "scale") # Do standardization of the data
)
# Get the predictions of your model in the test set
predictions = predict(rf_model, newdata = test)
# See the confusion matrix of your model in the test set
confusionMatrix(predictions, test$Species)
希望对您有所帮助
我是使用 Caret 包进行训练、验证和测试的初学者。我在网上进行了研究,发现它有些令人困惑(似乎大多数显示训练和测试都没有验证),你能展示一个代码示例来说明它是如何工作的吗?
train
允许您进行验证等等。您可以为 trControl
参数提供一个 trainControl
函数,允许您指定训练过程的细节。默认情况下 train
已经将您传入的数据的 75% 拆分为训练,25% 用于验证,您也可以在 trainControl
.
我建议您查看 train
和 trainControl
文档,here and here 以了解有关您可以在训练过程中指定的详细信息的更多信息。
下面是一个使用 5 折交叉验证训练随机森林并使用 Caret 和 train
函数对数据进行标准化以更好地举例说明的简单示例。
(注意:我添加了一些不必要的东西,例如 verboseIter = TRUE
和 classProbs = TRUE
只是为了向您展示一些使用插入符号获得的功能)
library(caret)
library(datasets)
# Loading the iris dataset
data(iris)
# Specifying an 80-20 train-test split
train_idx = createDataPartition(iris$Species, p = .8, list = F)
# Creating the training and testing sets
train = iris[train_idx, ]
test = iris[-train_idx, ]
# Declaring the trainControl function
train_ctrl = trainControl(
method = "cv", #Specifying Cross validation
number = 5, # Specifying 5-fold
verboseIter = TRUE, # So that each iteration you get an update of the progress
classProbs = TRUE # So that you can obtain the probabilities for each example
)
rf_model = train(
Species ~., # Specifying the response variable and the feature variables
method = "rf", # Specifying the model to use
data = train,
trControl = train_ctrl,
preProcess = c("center", "scale") # Do standardization of the data
)
# Get the predictions of your model in the test set
predictions = predict(rf_model, newdata = test)
# See the confusion matrix of your model in the test set
confusionMatrix(predictions, test$Species)
希望对您有所帮助