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)
print("best bandwidth: {0}".format(grid.best_estimator_.bandwidth))

really need your help, buddy!


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.

  • $\begingroup$ Thanks. I have two more questions: 1. Does "score gives the log-likelihood of the test data in the estimated density. " mean that the entire sample is used to build the KDE model for each potential hyperparameter value, and then the test data is used to calculate the log-likelihood of that KDE model, and finally the hyperparameter of the KDE model with the highest log-likelihood value is selected? 2. What's the "test data" here? How does the 5-fold cross-validation work? (you know, KDE modeling requires all samples, not just four-fifths of the data, so why split the data into five parts?) $\endgroup$ – Gid Dec 14 '20 at 1:48
  • $\begingroup$ @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). $\endgroup$ – Ben Reiniger Dec 14 '20 at 20:38
  • $\begingroup$ @ Ben Reiniger Thanks buddy. But I still don't understand, what I mean is that we only have one data set, so what data set is the 5 different KDEs created from? What is the "unseen-to-them data", and where do they come from? You know, for the KDE model, it needs all the data in the dataset, so we can't divide a unique dataset into five pieces $\endgroup$ – Gid Dec 15 '20 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.