R语言 重复K折交叉验证
重复K折是分类和回归机器学习模型中最受欢迎的交叉验证技术。对数据集进行多次洗牌和随机抽样是重复K折算法的核心程序,它的结果是建立一个稳健的模型,因为它涵盖了最大的训练和测试操作。这种交叉验证技术在评估机器学习模型的准确性方面的工作取决于两个参数。第一个参数是 K ,它是一个整数值,它表示给定的数据集将被分成K个折叠(或子集)。在这K个折叠中,模型在K-1个子集上进行训练,剩下的子集将被用来评估模型的性能。这些步骤将重复到一定的次数,这将由该算法的第二个参数决定,因此它被命名为重复K折,即 K折交叉验证算法被重复一定的次数。
重复K-折交叉验证法涉及的步骤。
重复K折的每一次迭代都是正常K折算法的实现。在K-折交叉验证技术中,涉及以下步骤。
- 将数据集随机分成K个子集
- 对于每一个制定的数据点子集
- 将该子集作为验证集
- 将所有其余的子集用于训练目的
- 训练模型并在验证集或测试集上对其进行评估
- 计算预测误差
- 重复上述步骤K次,即直到模型没有在所有子集上进行训练和测试。
- 通过取每个案例中预测误差的平均值来生成总体预测误差
因此,在重复的k-fold交叉验证方法中,上述步骤将在给定的数据集上重复一定的次数。在每一次迭代中,数据集会被分成完全不同的K折,模型的性能得分也会不同。最后,所有情况下的平均性能得分将给出模型的最终准确性。 为了执行重复K-fold方法的这些复杂任务,R语言提供了丰富的内置函数和软件包库。下面是在分类和回归机器学习模型上实现重复K折交叉验证技术的步骤。
在分类中实现重复K折交叉验证法
当目标变量是分类数据类型时,分类机器学习模型被用来预测类标签。在这个例子中,Naive Bayes算法将被用作概率分类器来预测目标变量的类别标签。
第1步:加载所需的包和库
所有必要的库和包都必须被导入,以便无差错地执行任务。下面是为重复K-fold算法设置R环境的代码。
# load the library
# package to perform data manipulation
# and visualization
library(tidyverse)
# package to compute
# cross - validation methods
library(caret)
# loading package to
# import desired dataset
library(ISLR)
第2步:探索数据集
在导入所需的库后,是时候在R环境中加载数据集了。探索数据集也是非常重要的,因为在使用数据集进行训练和测试之前,它可以让人知道是否需要对其进行任何修改。下面是执行这项任务的代码。
# assigning the complete dataset
# Smarket to a variable
dataset <- Smarket[complete.cases(Smarket), ]
# display the dataset with details
# like column name and its data type
# along with values in each row
glimpse(dataset)
# checking values present
# in the Direction column
# of the dataset
table(dataset$Direction)
输出
Rows: 1,250
Columns: 9
Year <dbl> 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, ... Lag1 <dbl> 0.381, 0.959, 1.032, -0.623, 0.614, 0.213, 1.392, -0.403, 0.027, 1.303, 0.287, -0.498, -0.189, 0.680, 0.701, -0.562, 0.546, -1...
Lag2 <dbl> -0.192, 0.381, 0.959, 1.032, -0.623, 0.614, 0.213, 1.392, -0.403, 0.027, 1.303, 0.287, -0.498, -0.189, 0.680, 0.701, -0.562, 0... Lag3 <dbl> -2.624, -0.192, 0.381, 0.959, 1.032, -0.623, 0.614, 0.213, 1.392, -0.403, 0.027, 1.303, 0.287, -0.498, -0.189, 0.680, 0.701, -...
Lag4 <dbl> -1.055, -2.624, -0.192, 0.381, 0.959, 1.032, -0.623, 0.614, 0.213, 1.392, -0.403, 0.027, 1.303, 0.287, -0.498, -0.189, 0.680, ... Lag5 <dbl> 5.010, -1.055, -2.624, -0.192, 0.381, 0.959, 1.032, -0.623, 0.614, 0.213, 1.392, -0.403, 0.027, 1.303, 0.287, -0.498, -0.189, ...
Volume <dbl> 1.19130, 1.29650, 1.41120, 1.27600, 1.20570, 1.34910, 1.44500, 1.40780, 1.16400, 1.23260, 1.30900, 1.25800, 1.09800, 1.05310, ... Today <dbl> 0.959, 1.032, -0.623, 0.614, 0.213, 1.392, -0.403, 0.027, 1.303, 0.287, -0.498, -0.189, 0.680, 0.701, -0.562, 0.546, -1.747, 0...
Direction <fct> Up, Up, Down, Up, Up, Up, Down, Up, Up, Up, Down, Down, Up, Up, Down, Up, Down, Up, Down, Down, Down, Down, Up, Down, Down, Up...
> table(datasetDirection)
Down Up
602 648
上述信息表明,数据集的自变量是 **< dbl>数据类型 **,即双精度浮点数。数据集的目标变量是 “方向”,它的数据类型是 **因子(
- 向下取样
- 向上取样
- 使用SMOTE和ROSE的混合取样
第3步:用重复K-fold算法建立模型
trainControl() 函数被定义为设置重复次数和K参数的值。之后,按照重复K折算法所涉及的步骤建立模型。下面是具体的实现。
# setting seed to generate a
# reproducible random sampling
set.seed(123)
# define training control which
# generates parameters that further
# control how models are created
train_control <- trainControl(method = "repeatedcv",
number = 10, repeats = 3)
# building the model and
# predicting the target variable
# as per the Naive Bayes classifier
model <- train(Direction~., data = dataset,
trControl = train_control, method = "nb")
第4步:评估模型的准确性
在这最后一步,模型的性能分数将在所有可能的验证褶皱上测试后生成。以下是打印所开发的模型的准确性和整体总结的代码。
# summarize results of the
# model after calculating
# prediction error in each case
print(model)
输出
Naive Bayes
1250 samples
8 predictor
2 classes: 'Down', 'Up'
No pre-processing
Resampling: Cross-Validated (10 fold, repeated 3 times)
Summary of sample sizes: 1124, 1125, 1126, 1125, 1125, 1126, ...
Resampling results across tuning parameters:
usekernel Accuracy Kappa
FALSE 0.9562616 0.9121273
TRUE 0.9696037 0.9390601
Tuning parameter 'fL' was held constant at a value of 0
Tuning parameter 'adjust' was held constant at a value of 1
Accuracy was used to select the optimal model using the largest value.
The final values used for the model were fL = 0, usekernel = TRUE and adjust = 1.
在回归中实现重复的K-折交叉验证法
回归机器学习模型是那些目标变量为连续性质的数据集的首选,如一个地区的温度,商品的成本等。目标变量的数值可以是整数或浮点数。以下是在回归模型中实现重复k-fold算法作为交叉验证技术所需的步骤。
第1步:加载数据集和所需软件包
作为第1步,R环境必须加载所有必要的包和库,以执行各种操作。下面是导入所有需要的库的代码。
# loading required packages
# package to perform data manipulation
# and visualization
library(tidyverse)
# package to compute
# cross - validation methods
library(caret)
第2步:加载和检查数据集
一旦所有的软件包被导入,就是加载所需数据集的时候了。这里的 “树木 “数据集被用于回归模型,它是R语言的一个内置数据集。此外,为了建立一个正确的模型,有必要了解数据集的结构。所有这些任务都可以用下面的代码来完成。
# access the data from R’s datasets package
data(trees)
# look at the first several rows of the data
head(trees)
输出
Girth Height Volume
1 8.3 70 10.3
2 8.6 65 10.3
3 8.8 63 10.2
4 10.5 72 16.4
5 10.7 81 18.8
6 10.8 83 19.7
第3步:用重复K折算法建立模型
trainControl() 函数被定义为设置重复次数和K参数的值。之后,按照重复K-fold算法涉及的步骤建立模型。下面是实现的过程。
# setting seed to generate a
# reproducible random sampling
set.seed(125)
# defining training control as
# repeated cross-validation and
# value of K is 10 and repetition is 3 times
train_control <- trainControl(method = "repeatedcv",
number = 10, repeats = 3)
# training the model by assigning sales column
# as target variable and rest other column
# as independent variable
model <- train(Volume ~., data = trees,
method = "lm",
trControl = train_control)
第4步:评估模型的准确性
按照重复K-fold技术的算法,模型要针对数据集的每一个独特的折叠(或子集)进行测试,在每一种情况下,都要计算预测误差,最后,所有预测误差的平均值被视为模型的最终性能得分。因此,下面是打印模型的最终得分和整体总结的代码。
# printing model performance metrics
# along with other details
print(model)
输出
Linear Regression
31 samples
2 predictor
No pre-processing
Resampling: Cross-Validated (10 fold, repeated 3 times)
Summary of sample sizes: 28, 28, 28, 29, 28, 28, ...
Resampling results:
RMSE Rsquared MAE
4.021691 0.957571 3.362063
Tuning parameter 'intercept' was held constant at a value of TRUE
重复K-折交叉验证的优点
- 一个非常有效的方法来估计模型的预测误差和准确性。
- 在每次重复中,数据样本被洗牌,从而形成不同的样本数据分割。
重复的K-折交叉验证的缺点
- 较低的K值会导致一个有偏见的模型,而较高的K值会导致模型性能指标的变化。因此,对模型使用正确的K值是至关重要的(一般来说,K=5和K=10是可取的)。
- 每次重复,算法都要从头开始训练模型,这意味着评估模型的计算时间随着重复次数的增加而增加。