"""Grid Search Optimizer, which generates all of the possible points to search for using a grid."""importitertoolsfromskopt.spaceimportInteger,Realfromevalml.tuners.tunerimportTunerfromevalml.tuners.tuner_exceptionsimportNoParamsException
[docs]classGridSearchTuner(Tuner):"""Grid Search Optimizer, which generates all of the possible points to search for using a grid. Args: pipeline_hyperparameter_ranges (dict): a set of hyperparameter ranges corresponding to a pipeline's parameters n_points (int): The number of points to sample from along each dimension defined in the ``space`` argument. Defaults to 10. random_seed (int): Seed for random number generator. Unused in this class, defaults to 0. Examples: >>> tuner = GridSearchTuner({'My Component': {'param a': [0.0, 10.0], 'param b': ['a', 'b', 'c']}}, n_points=5) >>> proposal = tuner.propose() ... >>> assert proposal.keys() == {'My Component'} >>> assert proposal['My Component'] == {'param a': 0.0, 'param b': 'a'} Determines points using a grid search approach. >>> for each in range(5): ... print(tuner.propose()) {'My Component': {'param a': 0.0, 'param b': 'b'}} {'My Component': {'param a': 0.0, 'param b': 'c'}} {'My Component': {'param a': 10.0, 'param b': 'a'}} {'My Component': {'param a': 10.0, 'param b': 'b'}} {'My Component': {'param a': 10.0, 'param b': 'c'}} """def__init__(self,pipeline_hyperparameter_ranges,n_points=10,random_seed=0):super().__init__(pipeline_hyperparameter_ranges,random_seed=random_seed)raw_dimensions=list()importpandasaspdpd.DataFrame().iterrows()fordimensioninself._search_space_ranges:# Categorical dimensionifisinstance(dimension,list):range_values=dimensionelifisinstance(dimension,(Real,Integer,tuple)):if(isinstance(dimension,(tuple))andisinstance(dimension[0],(int,float))andisinstance(dimension[1],(int,float))):ifdimension[1]>dimension[0]:low=dimension[0]high=dimension[1]else:error_text="Upper bound must be greater than lower bound. Parameter lower bound is {0} and upper bound is {1}"error_text=error_text.format(dimension[0],dimension[1])raiseValueError(error_text)else:low=dimension.lowhigh=dimension.highdelta=(high-low)/(n_points-1)ifisinstance(dimension,Integer):range_values=[int((x*delta)+low)forxinrange(n_points)]else:range_values=[(x*delta)+lowforxinrange(n_points)]raw_dimensions.append(range_values)self._grid_points=itertools.product(*raw_dimensions)self.curr_params=None
[docs]defadd(self,pipeline_parameters,score):"""Not applicable to grid search tuner as generated parameters are not dependent on scores of previous parameters. Args: pipeline_parameters (dict): a dict of the parameters used to evaluate a pipeline score (float): the score obtained by evaluating the pipeline with the provided parameters """pass
[docs]defpropose(self):"""Returns parameters from _grid_points iterations. If all possible combinations of parameters have been scored, then ``NoParamsException`` is raised. Returns: dict: proposed pipeline parameters """ifnotself.curr_params:self.is_search_space_exhausted()params=self.curr_paramsself.curr_params=Nonereturnself._convert_to_pipeline_parameters(params)
[docs]defis_search_space_exhausted(self):"""Checks if it is possible to generate a set of valid parameters. Stores generated parameters in ``self.curr_params`` to be returned by ``propose()``. Returns: bool: If no more valid parameters exists in the search space, return False. Raises: NoParamsException: If a search space is exhausted, then this exception is thrown. """try:self.curr_params=next(self._grid_points)returnFalseexceptStopIteration:raiseNoParamsException("Grid search has exhausted all possible parameters.",)returnTrue