超參數總覽

Python 超參數調校

Alex Scriven

Data Scientist

什麼是超參數

超參數:

  • 在建模前由「你」設定(像老式收音機的旋鈕)
    • 你也會「調整」超參數!
  • 演算法本身不會學到這些

有旋鈕的老式收音機

Python 超參數調校

隨機森林中的超參數

建立一個簡單的隨機森林分類器並印出:

rf_clf = RandomForestClassifier()
print(rf_clf)

RandomForestClassifier(n_estimators='warn', criterion='gini', 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_jobs=None, oob_score=False, random_state=None, verbose=0,bootstrap=True, class_weight=None, warm_start=False)

更多資訊:http://scikit-learn.org

Python 超參數調校

單一超參數

n_estimators 為例。

資料型別與預設值:

n_estimators : integer, optional (default=10)

定義:

森林中的樹數量。

Python 超參數調校

設定超參數

在建立估計器時設定部分超參數:

rf_clf = RandomForestClassifier(n_estimators=100, criterion='entropy')
print(rf_clf)
RandomForestClassifier(n_estimators=100, criterion='entropy',
            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_jobs=None,
            oob_score=False, random_state=None, verbose=0,bootstrap=True,
            class_weight=None, warm_start=False)
Python 超參數調校

羅吉斯回歸的超參數

找出 Logistic Regression 的超參數:

log_reg_clf = LogisticRegression()

print(log_reg_clf) LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, max_iter=100, multi_class='warn', n_jobs=None, penalty='l2', random_state=None, solver='warn', tol=0.0001, verbose=0, warm_start=False)

此演算法可調的超參數較少!

Python 超參數調校

超參數的重要性

 

有些超參數比其他更重要。

有些對模型效能沒有幫助:

以隨機森林分類器為例:

  • n_jobs
  • random_state
  • verbose

不是所有超參數都適合「訓練」。

Python 超參數調校

隨機森林:重要超參數

 

一些重要的超參數:

  • n_estimators(取較大值)
  • max_features(嘗試不同值)
  • max_depthmin_sample_leaf(與過擬合相關)
  • (可能)criterion

記住:這只是指引

Python 超參數調校

如何找出重要的超參數?

 

學習資源:

  • 學術論文
  • 可信來源的部落格與教學(如 DataCamp)
  • Scikit Learn 模組文件
  • 實戰經驗
Python 超參數調校

一起來練習吧!

Python 超參數調校

Preparing Video For Download...