sklearn —— GridSearchCV进行超参数优化

超参数:即不直接在估计器内学习的参数。在 scikit-learn 包中,它们作为估计器类中构造函数的参数进行传递。

超参数优化:搜索超参数空间以便获得最好交叉验证分数


sklearn.model_selection.GridSearchCV

通过参数网格上的交叉验证网格搜索对估算器的指定参数值进行详尽搜索。

常用参数列表

参数名称 参数类型 作用
estimator 估计器对象 指明估计器对象,如RandomForest和AdaBoost等
param_grid 词典或词典列表 带有参数名称(字符串)作为键的字典以及用作值的参数设置列表,即指定估计器需要优化的参数(该参数可以是一个范围值),如n_estimators和learning_rate等
scoring 字符串列表或字典 指定多个评估指标
n_jobs 整数型int 并行运行的作业数量。
cv 整数型int 交叉验证折叠的数量
verbose interger 消息的详细程度,数值越高,消息越详细

属性

属性名称 属性类型 作用
cv_results_ numpy(masked)ndarrays的字典 交叉验证的结果统计列表
best_estimator_ estimator或dict 由搜索选择的估算器
best_score_ float best_estimator的平均交叉验证分数
best_params_ 字典 在保持数据上给出最佳结果的参数设置。
best_index_ int cv_results_与最佳候选参数设置相对应的(阵列的)索引。
scorer_ 函数或字典 用于保存数据以选择模型的最佳参数
n_splits_ int 交叉验证拆分的数量(折叠/迭代)

举例

rf_est = RandomForestClassifier(random_state=0)

rf_param_grid = {'n_estimators': [500], 'min_samples_split': [2, 3], 'max_depth': [20],'learning_rate': [0.01, 0.1]}

rf_grid = model_selection.GridSearchCV(rf_est, rf_param_grid, n_jobs=25, cv=10, verbose=1)

其他超参数优化方法

超参数优化方法 作用
model_selection.ParameterGrid 每个参数的网格具有不连续的数值。
model_selection.ParameterSampler 从给定分布取样的参数上的发生器。
model_selection.RandomizedSearchCV 随机搜索超参数。
model_selection.fit_grid_point 在一组参数上运行拟合。

你可能感兴趣的:(sklearn)