開始使用免費開始

將資料切分為訓練與測試

你現在已經可以按照幾個簡單步驟,從頭到尾建立一個機器學習模型了!在接下來的章節裡,你會更深入探討建模的各種細節;目前先練習並理解關鍵步驟。

獨立特徵已載入為名為 Xpandas DataFrame,依變數則載入為名為 Ypandas Series。

此外,已從 sklearn 函式庫載入 train_test_split 函式。你現在將建立訓練與測試資料集,然後確認資料是否正確切分。

本練習屬於課程

Python 的行銷機器學習

檢視課程

練習說明

  • XY 切分為訓練集與測試集,其中 25% 的資料作為測試。
  • 確認訓練資料集只包含原始資料的 75%。
  • 確認測試資料集只包含原始資料的 25%。

動手互動練習

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

# Split X and Y into training and testing datasets
train_X, test_X, train_Y, test_Y = ___(___, ___, test_size=0.___)

# Ensure training dataset has only 75% of original X data
print(___.shape[0] / X.shape[0])

# Ensure testing dataset has only 25% of original X data
print(___.shape[0] / ___.shape[0])
編輯並執行程式碼