模型驗證導論

Python 的模型驗證

Kasey Jones

Data Scientist

什麼是模型驗證?

模型驗證包含:

  • 確認模型在新資料上表現如預期
  • 在保留資料集上測試模型表現
  • 選擇最佳模型、參數與評估指標
  • 在給定資料下達到最佳準確度
Python 的模型驗證

scikit-learn 建模複習

基本建模步驟:

model = RandomForestRegressor(n_estimators=500, random_state=1111)

model.fit(X=X_train, y=y_train)
RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=None,
           max_features='auto', max_leaf_nodes=None,
           min_impurity_decrease=0.0, min_impurity_split=None,
           min_samples_leaf=1, min_samples_split=2,
           min_weight_fraction_leaf=0.0, n_estimators=500, n_jobs=1,
           oob_score=False, random_state=1111, verbose=0, warm_start=False)
Python 的模型驗證

建模複習(續)

predictions = model.predict(X_test)

print("{0:.2f}".format(mae(y_true=y_test, y_pred=predictions)))
10.84

平均絕對誤差公式

$$ \frac{\sum_{i=1}^{n} |y_i - \hat{y}_i|}{n} $$

Python 的模型驗證

先修複習

Python 的模型驗證

Fivethirtyeight 提供多個資料集,包括萬聖節糖果戰力排名。每種糖果都有 0 到 100% 的對戰勝率。

Python 的模型驗證

看過 vs. 未看過的資料

訓練資料 = 看過的資料

model = RandomForestRegressor(n_estimators=500, random_state=1111)
model.fit(X_train, y_train)
train_predictions = model.predict(X_train)

測試資料 = 未看過的資料

model = RandomForestRegressor(n_estimators=500, random_state=1111)
model.fit(X_train, y_train)
test_predictions = model.predict(X_test)
Python 的模型驗證

開始吧!

Python 的模型驗證

Preparing Video For Download...