开始使用免费开始使用

训练集与测试集划分

在规范的机器学习流程中,必须将一部分数据(测试数据)从任何决策过程中剥离出来。这样在模型定型后,您就能独立评估其性能。其余数据即为训练数据,用于构建并选择最佳模型。

在本练习中,您将使用 rsample 包对 gapminder 数据进行初始的训练-测试划分。

注意: 由于是随机划分,划分前设置随机种子是一个良好实践。

本练习是课程的一部分

Tidyverse 中的机器学习

查看课程

练习说明

  • 使用 initial_split() 函数将数据划分为 75% 训练集与 25% 测试集,并赋值给 gap_split
  • 使用 training() 函数从 gap_split 中提取训练数据框。
  • 使用 testing() 函数从 gap_split 中提取测试数据框。
  • 使用 dim() 分别作用于 training_datatesting_data,确认新数据框的维度符合预期。

交互式实操练习

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

set.seed(42)

# Prepare the initial split object
gap_split <- initial_split(___, prop = ___)

# Extract the training data frame
training_data <- ___

# Extract the testing data frame
testing_data <- ___

# Calculate the dimensions of both training_data and testing_data
dim(___)
dim(___)
编辑并运行代码