开始使用免费开始使用

交叉验证数据框

现在您已经留出一部分数据作为 testing data,可以用其余数据来寻找表现最好的模型。

在本练习中,您将使用 rsample 包中的 vfold_cv() 函数,把训练数据拆分为 5 组 train-validate 集合。

本练习是课程的一部分

Tidyverse 中的机器学习

查看课程

练习说明

  • 使用 vfold_cv() 基于 training_data 构建一个用于 5 折交叉验证的数据框,并将其赋值给 cv_split
  • 通过为 cv_split 添加两列来准备 cv_data
    • train:在 splits 列上映射 training(),得到各折的训练数据框。
    • validate:在 splits 列上映射 testing(),得到各折的验证数据框。

交互式实操练习

通过完成这段示例代码来试试这个练习。

set.seed(42)

# Prepare the data frame containing the cross validation partitions
cv_split <- vfold_cv(___, v = ___)

cv_data <- cv_split %>% 
  mutate(
    # Extract the train data frame for each split
    train = map(___, ~___(.x)), 
    # Extract the validate data frame for each split
    validate = map(___, ~___(.x))
  )

# Use head() to preview cv_data
head(cv_data)
编辑并运行代码