Sklearn调参之sklearn.model_selection.GridSearchCV

对估计器的指定参数值穷举搜索。寻找合适的参数,GRIDSCACHCV实现了一个“拟合”和“得分”方法。在所使用的估计器中实现了“预测”、“预测函数”、“决策函数”、“变换”和“逆变换”。应用这些方法的估计器的参数通过参数网格上的交叉验证网格搜索来优化。

用法

sklearn.model_selection.GridSearchCV(estimator, param_grid, scoring=None, fit_params=None, n_jobs=1,iid=True, refit=True, cv=None, verbose=0, pre_dispatch=‘2*n_jobs’, error_score=’raise’, return_train_score=’warn’)

estimator :estimator object

这被假定为实现SCIKIT学习估计器接口。

param_grid : dict or list of dictionaries

字典中使用参数名(字符串)作为键和参数设置的列表来尝试作为值,或者这样的字典的列表,在这种情况下,遍历列表中的每个字典的网格。这使得能够对任何参数设置序列进行搜索。

scoring : string, callable, list/tuple, dict or None, default: None

如果没有,则使用估计器的默认记分器(如果可用)。

fit_params : dict, optional

传递给FIT方法的参数。自0.19版以来,Fista PARAMS作为一个构造函数参数在0.19版本中被弃用,将在版本0.21中被删除。Pass fit parameters to the fit method instead.

n_jobs : int, default=1

并行运行的核数。

pre_dispatch : int, or string, optional

控制并行执行期间分派的作业数量。减少这个数目可以避免在比CPU能够处理的更多任务被分配时避免内存消耗的爆炸。这个参数可以是:

没有,在这种情况下,所有的工作都立即被创造和产生。使用此操作轻量级和快速运行的作业,以避免由于按需生成作业而导致的延迟。

一个int,给出产生的总工作的确切数量。

一个字符串,将表达式作为NSJOB的函数,如“2×N-JOB”

iid : boolean, default=True

如果是真的,假设数据在褶皱上是相同分布的,损失最小的是每个样本的总损失,而不是整个褶皱的平均损失。

cv : int, cross-validation generator or an iterable, optional

确定交叉验证拆分策略。CV的可能输入是:没有,使用默认的3倍交叉验证,

整数,以指定折叠的数量(分层)k襞,用作交叉验证生成器的对象。

refit : boolean, or string, default=True

在整个数据集上使用最佳找到的参数重新估计。

对于多指标评价,这需要是一个字符串,使用记分器来寻找最佳的参数,用于重新估计估计器的末尾。重新编译的估计器在BestyRealSturix属性中可用,并且允许直接在GRIDSCACHCV实例上使用预测。此外,对于多度量评估,属性BestJoixxx,BestJoSeCype和BestHythPrimeSt*将仅在重新设置时可用,所有这些都将被确定为特定的记分器。

verbose : integer

控制冗长:越高,信息越多。

error_score : ‘raise’ (default) or numeric

如果在估计器拟合中出现错误,则将其赋值给分数。如果设置为“raise”,则会引发错误。如果给定数值,则FitFailedWarning被提升。此参数不影响重新配置步骤,它总是会引发错误。

return_train_score : boolean, optional

方法

best_estimator_ : estimator or dict

Estimator that was chosen by the search, i.e. estimator which gave highest score (or smallest loss if specified) on the left out data. Not available if refit=Falese

由搜索选择的估计器,即估计器,它在左出数据上给出最高得分(或指定的最小损失)。如果RIFIT=false,则不可用。

See refit parameter for more information on allowed values.

best_score_ : float

Mean cross-validated score of the best_estimator

For multi-metric evaluation, this is present only if refit is specified.

best_params_ : dict

Parameter setting that gave the best results on the hold out data.

For multi-metric evaluation, this is present only if refit is specified.

使用例子

from sklearn.grid_search import GridSearchCV

或者

from sklearn.model_selection import GridSearchCV

#运行的结果好像并没有什么不同= =

params = {'max_depth':[x for x in range(2,7)], 'n_estimators':[y for y in range(100, 1100, 200) ],'learning_rate':[0.05, 0.1, 0.25, 0.5, 1.0]}

xgbc_best =XGBClassifier()

gs = GridSearchCV(xgbc_best, params, n_jobs=-1, cv=5, verbose=1)

gs.fit(X_train, y_train)

print(gs.best_score_)#最好的得分

print(gs.best_params_)#最好的参数

更多详情参见文档
————————————————
版权声明:本文为CSDN博主「ustbclearwang」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ustbclearwang/article/details/81484778

原文地址:https://www.cnblogs.com/liunaiming/p/12082146.html