開始使用免費開始

將模型擬合到訓練資料

現在要把資料切分為「訓練集」用來擬合模型,以及獨立的「測試集」用來評估模型的預測能力。不過在切分之前,先對 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(___:___)
編輯並執行程式碼