import numpy as np
[docs]def make_grid(n_grid, z_min, z_max):
"""Create grid of equally spaced points
:param n_grid: integer number of grid points
:param z_min: float, the minimum value of the z box
:param z_max: float, the maximum value of the z box
:returns: a grid of n_grid equally spaced points between z_min and z_max
"""
return np.linspace(z_min, z_max, n_grid).reshape((n_grid, 1))
[docs]def params_dict_optim_decision(params, multi_output=False):
"""
Ingest parameter dictionary and determines whether to do CV optimization.
If one of the parameter has a list of length above 1 as values
then automatically format the dictionary for GridSearchCV.
:param params: dictionary of model parameters
:param multi_output: boolean flag, whether the optimization would need
to be performed in MultiOutputRegressor
:returns: a dictionary of parameters and a boolean flag of whether CV-opt
is going to be performed. If CV-optimization is set to happen then
the paramater dictionary is correctly format.
"""
# Determines whether there are any list in the items of the dictionary
opt_flag = False
for k, value in params.items():
if type(value) == tuple:
raise ValueError(
"Parameter values need to be lists or np.array, not tuple."
"Current issues with parameter %s" % (k)
)
if type(value) == list or type(value) == np.ndarray:
opt_flag = True
break
# Format the dictionary if necessary - put int, str and float into a list
# with one element
out_param_dict = {} if opt_flag else params.copy()
if opt_flag:
for k, value in params.items():
out_value = value.tolist() if type(value) == np.ndarray else value
out_value = [out_value] if type(out_value) != list else out_value
out_key = "estimator__" + k if multi_output else k
out_param_dict[out_key] = out_value
return out_param_dict, opt_flag