超参数概览

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 中的超参数调优

逻辑回归中的超参数

查看逻辑回归的超参数:

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 中的超参数调优

Passons à la pratique !

Python 中的超参数调优

Preparing Video For Download...