# How does GridSearchCV(KernelDensity(), Params) find the optimal bandwidth?

I wanna know How GridSearchCV works? I mean this method gives a grid interval for the optional bandwidth params = {'bandwidth': np.linspace(0.1, 1, 100)}, but how does it evaluate each bandwidth value? In other words, why is the selected bandwidth the optimal bandwidth? What is the evaluation function of the optimal bandwidth? The optimal bandwidth code for grid search is as follows:

params = {'bandwidth': np.linspace(0.1, 1, 100)}
grid = GridSearchCV(KernelDensity(), params)
grid.fit(data)
print("best bandwidth: {0}".format(grid.best_estimator_.bandwidth))


GridSearchCV in general performs cross-validation (by default, 5-fold), and (by default) selects the set of hyperparameter values that give the best performance (on average across the 5 test folds). It (by default) uses the estimator's score method to evaluation performance on the test folds. In the case of KernelDensity, score gives the log-likelihood of the test data in the estimated density.
• @Gid It's the usual cross-validation approach: 5 different KDEs are created and scored on unseen-to-them data, those scores averaged, and then the hyperparameter giving the highest of these averages is selected. Then a final KDE is fitted using that hyperparameter on the entire training dataset (if using the default in sklearn refit=True). – Ben Reiniger Dec 14 '20 at 20:38