bayes_opt.BayesianOptimization

class bayes_opt.BayesianOptimization(f: collections.abc.Callable[..., float] | None, pbounds: Mapping[str, tuple[float, float]], acquisition_function: AcquisitionFunction | None = None, constraint: NonlinearConstraint | None = None, random_state: int | RandomState | None = None, verbose: int = 2, bounds_transformer: DomainTransformer | None = None, allow_duplicate_points: bool = False)

Handle optimization of a target function over a specific target space.

This class takes the function to optimize as well as the parameters bounds in order to find which values for the parameters yield the maximum value using bayesian optimization.

Parameters:
f : function or None.

Function to be maximized.

pbounds : dict

Dictionary with parameters names as keys and a tuple with minimum and maximum values.

constraint : NonlinearConstraint.

Note that the names of arguments of the constraint function and of f need to be the same.

random_state : int or numpy.random.RandomState, optional(default=None)

If the value is an integer, it is used as the seed for creating a numpy.random.RandomState. Otherwise the random state provided is used. When set to None, an unseeded random state is generated.

verbose : int, optional(default=2)

The level of verbosity.

bounds_transformer : DomainTransformer, optional(default=None)

If provided, the transformation is applied to the bounds.

allow_duplicate_points : bool, optional (default=False)

If True, the optimizer will allow duplicate points to be registered. This behavior may be desired in high noise situations where repeatedly probing the same point will give different answers. In other situations, the acquisition may occasionally generate a duplicate point.

acquisition_function : AcquisitionFunction | None

property acquisition_function : AcquisitionFunction

Return the acquisition function associated with the optimizer.

property constraint : ConstraintModel | None

Return the constraint associated with the optimizer, if any.

property max : dict[str, Any] | None

Get maximum target value found and corresponding parameters.

See TargetSpace.max for more information.

maximize(init_points: int = 5, n_iter: int = 25) None

Maximize the given function over the target space.

Parameters:
init_points : int, optional(default=5)

Number of random points to probe before starting the optimization.

n_iter : int, optional(default=25)

Number of iterations where the method attempts to find the maximum value.

Return type:

None

Warning

The maximize loop only fits the GP when suggesting a new point to probe based on the acquisition function. This means that the GP may not be fitted on all points registered to the target space when the method completes. If you intend to use the GP model after the optimization routine, make sure to fit it manually, e.g. by calling optimizer._gp.fit(optimizer.space.params, optimizer.space.target).

probe(params: Mapping[str, float] | Sequence[float] | ndarray[Any, dtype[floating[Any]]], lazy: bool = True) None

Evaluate the function at the given points.

Useful to guide the optimizer.

Parameters:
params : dict or list

The parameters where the optimizer will evaluate the function.

lazy : bool, optional(default=True)

If True, the optimizer will evaluate the points when calling maximize(). Otherwise it will evaluate it at the moment.

Return type:

None

register(params: Mapping[str, float] | Sequence[float] | ndarray[Any, dtype[floating[Any]]], target: float, constraint_value: float | ndarray[Any, dtype[floating[Any]]] | None = None) None

Register an observation with known target.

Parameters:
params : dict or list

The parameters associated with the observation.

target : float

Value of the target function at the observation.

constraint_value : float or None

Value of the constraint function at the observation, if any.

Return type:

None

property res : list[dict[str, Any]]

Get all target values and constraint fulfillment for all parameters.

See TargetSpace.res for more information.

set_bounds(new_bounds: Mapping[str, ndarray[Any, dtype[floating[Any]]] | Sequence[float]]) None

Modify the bounds of the search space.

Parameters:
new_bounds : dict

A dictionary with the parameter name and its new bounds

Return type:

None

set_gp_params(**params: Any) None

Set parameters of the internal Gaussian Process Regressor.

Return type:

None

Parameters:
params : Any

property space : TargetSpace

Return the target space associated with the optimizer.

suggest() dict[str, float]

Suggest a promising point to probe next.

Return type:

dict[str, float]