開始使用免費開始

切分為訓練與測試

現在我們已經有一個 dataframe,可以開始套用標準的建模技巧。 在本練習中,你會把資料切分為訓練集與測試集。

本練習屬於課程

使用 R 進行網路化資料的預測分析

檢視課程

練習說明

  • 為了讓結果可以重現,使用 set.seed() 將隨機種子設為 7。
  • 使用 sample()studentnetworkdata 總列數所對應的序列中,隨機抽取三分之二的數字,並將此向量命名為 index_train
  • 建立訓練集:包含 studentnetworkdata 中索引位於 index_train 的列,命名為 training_set
  • 建立測試集:排除 studentnetworkdata 中索引位於 index_train 的列,命名為 test_set

動手互動練習

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

# Set the seed
set.seed(___)

# Creat the index vector
index_train <- sample(1:nrow(___), 2 / 3 * nrow(___))

# Make the training set
training_set <- ___[index_train,]

# Make the test set
___ <- ___[-index_train,]
編輯並執行程式碼