如何使用 GridSearchCV 确定最优参数
How to use optimal parameters identified GridSearchCV
我正在使用GridSearchCV
来确定最优参数,但我不确定如何实际使用最优参数,也就是说,在下面的代码中,在第三行中,我的结果在改变后并没有改变第一行(例如,如果我更改参数 space,或将 recall 替换为 precision 等)
cv = GridSearchCV(pipeline, parameters, cv=len(range(2014,2019)), scoring='recall', refit=True)
cv.fit(X,y)
y_pred = cross_val_predict(cv, X, y, cv=len(range(2014,2019)))
有没有办法确保,无论何时我调用 cv.predict
,在 GridSearchCV
中确定的任何最佳参数实际上也会继续使用?
根据文档 here,它说:
predict(x)
Call predict on the estimator with the best found parameters.
本质上是在拟合之后,调用predict使用最优估计器。如果你想仔细检查你的参数,你可以在拟合后看到最好的参数存储在 class 属性中 best_params_
.
至于为什么你的结果在改变第一行后没有改变,我猜想它恰好达到了相同的最佳参数:或者有多个最佳参数。
默认情况下,GridSearchCV
已经完成了您想要的操作,除非您设置 refit=False
。正如 GridSearchCV
documentation:
所述
refit : boolean, or string, default=True
Refit an estimator using the best found parameters on the whole dataset.
For multiple metric evaluation, this needs to be a string denoting the scorer is used to find the best parameters for refitting the estimator at the end.
The refitted estimator is made available at the best_estimator_
attribute and permits using predict
directly on this GridSearchCV
instance.
Also for multiple metric evaluation, the attributes best_index_
, best_score_
and best_params_
will only be available if refit is set and all of them will be determined w.r.t this specific scorer.
See scoring parameter to know more about multiple metric evaluation.
因此,每当您调用 cv.predict
时,经过改装的最佳估计器就会做出预测。
在您的情况下,结果相同可能是因为最佳估算器在 所有指标 上得分最高。
我正在使用GridSearchCV
来确定最优参数,但我不确定如何实际使用最优参数,也就是说,在下面的代码中,在第三行中,我的结果在改变后并没有改变第一行(例如,如果我更改参数 space,或将 recall 替换为 precision 等)
cv = GridSearchCV(pipeline, parameters, cv=len(range(2014,2019)), scoring='recall', refit=True)
cv.fit(X,y)
y_pred = cross_val_predict(cv, X, y, cv=len(range(2014,2019)))
有没有办法确保,无论何时我调用 cv.predict
,在 GridSearchCV
中确定的任何最佳参数实际上也会继续使用?
根据文档 here,它说:
predict(x)
Call predict on the estimator with the best found parameters.
本质上是在拟合之后,调用predict使用最优估计器。如果你想仔细检查你的参数,你可以在拟合后看到最好的参数存储在 class 属性中 best_params_
.
至于为什么你的结果在改变第一行后没有改变,我猜想它恰好达到了相同的最佳参数:或者有多个最佳参数。
GridSearchCV
已经完成了您想要的操作,除非您设置 refit=False
。正如 GridSearchCV
documentation:
refit : boolean, or string, default=True
Refit an estimator using the best found parameters on the whole dataset.For multiple metric evaluation, this needs to be a string denoting the scorer is used to find the best parameters for refitting the estimator at the end.
The refitted estimator is made available at the
best_estimator_
attribute and permits usingpredict
directly on thisGridSearchCV
instance.Also for multiple metric evaluation, the attributes
best_index_
,best_score_
andbest_params_
will only be available if refit is set and all of them will be determined w.r.t this specific scorer.See scoring parameter to know more about multiple metric evaluation.
因此,每当您调用 cv.predict
时,经过改装的最佳估计器就会做出预测。
在您的情况下,结果相同可能是因为最佳估算器在 所有指标 上得分最高。