開始使用免費開始

訓練與測試分割

在有紀律的機器學習流程中,關鍵步驟之一是保留一部分資料作為(測試資料),並且不參與任何決策過程。如此才能在模型定稿後,獨立評估模型的表現。其餘資料則作為訓練資料,用來建立並挑選最佳模型。

在本練習中,你將使用 rsample 套件,將 gapminder 資料做第一次的訓練-測試分割。

注意: 由於這是隨機分割,建議在分割前先設定隨機種子(seed)。

本練習屬於課程

Tidyverse 的 Machine Learning

檢視課程

練習說明

  • 使用 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(___)
編輯並執行程式碼