開始使用免費開始

試試 60/40 切分

如同影片所示,你將在本章使用 Sonar 資料集,並採用 60% 訓練集與 40% 測試集。我們再練習一次如何進行 train/test 切分,確保你已經熟悉流程。回想一下,你可以用 sample() 來取得資料列索引的隨機排列,供進行 train/test 切分時使用,例如:

n_obs <- nrow(my_data)
permuted_rows <- sample(n_obs)

接著使用這些列索引來隨機重新排序資料集,例如:

my_data <- my_data[permuted_rows, ]

當資料集已經隨機排序後,你可以將前面 60% 當作訓練集,最後 40% 當作測試集。

本練習屬於課程

使用 R 的 caret 進行 Machine Learning

檢視課程

練習說明

  • 取得 Sonar 的觀測數(列數),指定給 n_obs
  • 隨機打亂 Sonar 的列索引,並將結果存到 permuted_rows
  • 使用 permuted_rows 隨機重排 Sonar 的列,存為 Sonar_shuffled
  • 找出進行 60/40 切分時應該分割的列號,將此列號存為 split
  • Sonar_shuffled 的前 60% 存為訓練集。
  • Sonar_shuffled 的最後 40% 存為測試集。

動手互動練習

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

# Get the number of observations


# Shuffle row indices: permuted_rows


# Randomly order data: Sonar


# Identify row to split on: split
split <- round(n_obs * ___)

# Create train


# Create test
編輯並執行程式碼