How is best_score_ actually calculated in GridSearchCV?

Viewed 60

I'm doing a project and I needed to estimate the density function of a distribution. So I used 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,
   )

I printed print(grid.best_params_, ":", grid.best_score_)

But best_score gives me values between 16 with cv = 2 or 800 with cv = 50. I really don't understand what best_score_ means because in the library it says that:

best_score_float

 Mean cross-validated score of the best_estimator

How is it really calculated? Do we look for large or small values of best_estimator to have a better fit?

Thank you

1 Answers

Since you did not pass a scorer to GridSearchCV, it defaults to the scoring in the estimator. In your case, it is the total loglikelihood, see help page for kernelDensity. You can check it using an example below:

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
Related