【问题标题】:How is best_score_ actually calculated in GridSearchCV?best_score_ 是如何在 GridSearchCV 中实际计算的?
【发布时间】:2022-01-04 04:57:36
【问题描述】:

我正在做一个项目,我需要估计分布的密度函数。所以我使用了 GridSearchCV:

param_grid = {'kernel': ['gaussian', 'epanechnikov', 'exponential', 'linear', 'tophat', 'cosine'], 'bandwidth': np.linspace (0.01, .5, 1000)}

grid = GridSearchCV (
     estimator = KernelDensity (),
     param_grid = param_grid,
     n_jobs = -1,
     cv = 2,
     verbose = 0,
   )

我打印了 print(grid.best_params_, ":", grid.best_score_)

但是 best_score 给我的值在 16 和 cv = 2 或 800 和 cv = 50 之间。我真的不明白 best_score_ 是什么意思,因为在库中它说:

best_score_float

 Mean cross-validated score of the best_estimator

它是如何计算的?我们是在寻找 best_estimator 的大值还是小值以获得更好的拟合?

谢谢

【问题讨论】:

  • 超参数搜索总是试图最大化分数(很容易从cv_results_检查);因此像 negative 均方误差之类的东西。

标签: python scikit-learn gridsearchcv


【解决方案1】:

由于您没有将评分器传递给 GridSearchCV,因此它默认为估算器中的评分。在您的情况下,它是总对数似然,请参阅help page for kernelDensity。您可以使用以下示例进行检查:

from sklearn.model_selection import KFold

param_grid = {'kernel': ['gaussian'], 
'bandwidth': [1.0]}

kf = KFold(n_splits=2)
X = np.random.uniform(0,1,1000).reshape(-1,1)

grid = GridSearchCV (
     estimator = KernelDensity (),
     param_grid = param_grid,
     n_jobs = -1,
     cv = kf,
     verbose = 0,
   )

grid.fit(X)

grid.cv_results_['mean_test_score']
array([-497.83585627])

scores = []
for train_index, test_index in kf.split(X):
    ker = KernelDensity().fit(X[train_index])
    scores.append(ker.score(X[test_index]))

np.mean(scores)
-497.8358562692174

【讨论】:

    猜你喜欢
    • 2014-07-28
    • 2018-04-16
    • 2017-01-21
    • 2021-06-20
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    相关资源
    最近更新 更多