开始使用免费开始使用

将模型拟合到训练数据

现在需要将数据划分为一个用于拟合模型的"训练"集,以及一个用于评估模型预测能力的单独"测试"集。在拆分之前,先对 house_prices 的 100% 行进行不放回抽样,并将结果赋给 house_prices_shuffled。这样可以"打乱"行顺序,从而确保训练集和测试集是随机抽样得到的。

本练习是课程的一部分

Tidyverse 的数据建模

查看课程

交互式实操练习

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

# Set random number generator seed value for reproducibility
set.seed(76)

# Randomly reorder the rows
house_prices_shuffled <- house_prices %>% 
  sample_frac(size = 1, replace = FALSE)

# Train/test split
train <- house_prices_shuffled %>%
  slice(___:___)
test <- house_prices_shuffled %>%
  slice(___:___)
编辑并运行代码