超参数调优简介

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 中的模型验证

通用指南

  • 从基本项开始
  • 阅读文档
  • 测试合理范围
Python 中的模型验证

¡Vamos a practicar!

Python 中的模型验证

Preparing Video For Download...