建立訓練、測試與驗證資料集

Python 的模型驗證

Kasey Jones

Data Scientist

傳統訓練/測試切分

  • 已見資料(用於訓練)
  • 未見資料(不可用於訓練)

切分資料就是把一部分可用資料拿來訓練,較小一部分作為測試資料集。

Python 的模型驗證

資料集定義與比例

資料集 定義
Train 用於擬合模型的資料樣本
Test(保留樣本) 用於評估模型效能的資料樣本

比例範例

  • 80:20
  • 90:10(當資料很少時使用)
  • 70:30(當模型計算昂貴時使用)
Python 的模型驗證

X 與 y 資料集

import pandas as pd

tic_tac_toe = pd.read_csv("tic-tac-toe.csv")
X = pd.get_dummies(tic_tac_toe.iloc[:,0:9])
y = tic_tac_toe.iloc[:, 9]

涵蓋虛擬變數的 Python 課程:

Python 的模型驗證

建立保留樣本(holdout)

X_train, X_test, y_train, y_test  =\
    train_test_split(X, y, test_size=0.2, random_state=1111)

參數:

  • test_size
  • train_size
  • random_state
Python 的模型驗證

用於初步測試的資料集?

當要測試不同的模型參數時該怎麼做?

  • 100 與 1000 棵樹的比較
Python 的模型驗證

要測試模型參數,需要將可用資料分成三部分:訓練、驗證與測試。

Python 的模型驗證

訓練、驗證、測試(續)

X_temp, X_test, y_temp, y_test  =\
    train_test_split(X, y, test_size=0.2, random_state=1111)
X_train, X_val, y_train, y_val =\
    train_test_split(X_temp, y_temp, test_size=0.25, random_state=11111)
Python 的模型驗證

進入保留測試時間

Python 的模型驗證

Preparing Video For Download...