開始使用免費開始

建立訓練與測試資料集

將資料集分成訓練集與測試集,是建立與測試分類模型時的重要步驟。訓練集用來建立模型,測試集用來評估模型的預測準確度。

在這個練習中,你會把上一章建立的資料集切分為訓練集與測試集。資料集已載入到 df 資料框,且已設定好隨機種子以確保結果可重現。回想上一部影片中,我們用了一些方便的函式來設定訓練集長度的上限——現在換你來實作!

本練習屬於課程

R 的支援向量機

檢視課程

練習說明

  • 決定訓練集中列數的上限,並將其儲存在 sample_size
  • 建立向量 train,依照 80/20 的比例隨機指定訓練集。
  • 將位於 train 向量中的列指派給資料框 trainset,其餘列指派給資料框 testset

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Set the upper bound for the length of the training set
sample_size <- ___(___ * nrow(df))

# Assign rows to training set randomly
train <- ___(seq_len(nrow(df)), size = ___)

# Yield training and test sets
trainset <- df[___, ]
testset <- df[-___, ]
編輯並執行程式碼