超參數調校入門

Python 的模型驗證

Kasey Jones

Data Scientist

模型參數

參數是:

  • 從資料中學得或估計
  • 擬合模型的結果
  • 用於未來預測
  • 非手動設定
Python 的模型驗證

線性迴歸的參數

參數是在擬合模型時產生:

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(X, y)
print(lr.coef_, lr.intercept_)
[[0.798, 0.452]] [1.786]
Python 的模型驗證

線性迴歸的參數

在模型擬合前,參數不存在:

lr = LinearRegression()
print(lr.coef_, lr.intercept_)
AttributeError: 'LinearRegression' object has no attribute 'coef_'
Python 的模型驗證

模型超參數

超參數:

  • 在訓練前手動設定
  • 用來指定訓練方式
Python 的模型驗證

隨機森林的超參數

超參數 說明 可能值(預設)
n_estimators 森林中的決策樹數量 2+(10)
max_depth 決策樹的最大深度 2+(None)
max_features 分裂時要考慮的特徵數 參見文件
min_samples_split 允許分裂所需的最少樣本數 2+(2)
Python 的模型驗證

什麼是超參數調校?

超參數調校:

  • 選定超參數
  • 對同一模型以不同參數組合執行
  • 建立可挑選的參數範圍
  • 指定單一準確率指標
Python 的模型驗證

設定參數範圍

depth = [4, 6, 8, 10, 12]
samples = [2, 4, 6, 8]
features = [2, 4, 6, 8, 10]

# 指定超參數 rfc = RandomForestRegressor( n_estimators=100, max_depth=depth[0], min_samples_split=samples[3], max_features=features[1])
rfr.get_params()
{'bootstrap': True,
 'criterion': 'mse'
 ...
}
Python 的模型驗證

超參數太多了!

rfr.get_params()
{'bootstrap': True,
 'criterion': 'mse',
 'max_depth': 4,
 'max_features': 4,
 'max_leaf_nodes': None,
 'min_impurity_decrease': 0.0,
 'min_impurity_split': None,
 'min_samples_leaf': 1,
 'min_samples_split': 8,
 ...
 }
Python 的模型驗證

通用指引

  • Start with the basics
  • Read through the documentation
  • Test practical ranges
Python 的模型驗證

一起來練習吧!

Python 的模型驗證

Preparing Video For Download...