# GridSearchCV with custom tune grid

What is the best way to perform custom parameter search CV with the Scikit-learn API? I really like GridSearchCV. However for my case the param_grid parameter is inflexible because it will search over the entire span of parameter combinations. Ideally, I would like to provide my own parameter space in a dataframe, one column for each parameter.

Thus, is there an appropriate class within Scikit-learn to help me achieve this result?

GridSearchCV does allow param_grid to be a list of grid-dicts, which sometimes is sufficient. In this case, separate grids are generated and their union is searched.

There isn't quite a convenient implementation by which you provide your own list of hyperparameter points. But looking at the source code for GridSearchCV, you'll notice that it's amazingly sparse. Most everything is handled by the inherited class BaseSearchCV. The grid-specific stuff is also factored out:

    def _run_search(self, evaluate_candidates):
"""Search all candidates in param_grid"""
evaluate_candidates(ParameterGrid(self.param_grid))


The ParameterGrid class is what's responsible for turning the (possibly list of) dict(s) into a grid(s).

So you could probably easily write a custom class, inheriting from BaseSearchCV, where you pass the set of hyperparameter points you want and define _run_search to just evaluate each element from your set. In fact, already evaluate_candidates is defined assuming that the hyperparameter points live in an iterable candidate_params; the ParameterGrid does all the hard work there, but in your case you can just feed your list directly.

• Thank you for your comment! Most probably BaseSearchCV is the class I was looking for Jan 8, 2021 at 22:37