bayes_opt.TargetSpace
¶
-
class bayes_opt.TargetSpace(target_func: collections.abc.Callable[..., float] | None, pbounds: Mapping[str, tuple[float, float]], constraint: ConstraintModel | None =
None
, random_state: int | RandomState | None =None
, allow_duplicate_points: bool | None =False
) None ¶ Holds the param-space coordinates (X) and target values (Y).
Allows for constant-time appends.
- Parameters:¶
- target_func : function or None.¶
Function to be maximized.
- pbounds : dict¶
Dictionary with parameters names as keys and a tuple with minimum and maximum values.
- random_state : int, RandomState, or None¶
optionally specify a seed for a random number generator
- 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.
- constraint : ConstraintModel | None¶
Examples
>>> def target_func(p1, p2): >>> return p1 + p2 >>> pbounds = {"p1": (0, 1), "p2": (1, 100)} >>> space = TargetSpace(target_func, pbounds, random_state=0) >>> x = np.array([4, 5]) >>> y = target_func(x) >>> space.register(x, y) >>> assert self.max()["target"] == 9 >>> assert self.max()["params"] == {"p1": 1.0, "p2": 2.0}
- array_to_params(x: ndarray[Any, dtype[floating[Any]]]) dict[str, float] ¶
Convert an array representation of parameters into a dict version.
- property bounds : ndarray[Any, dtype[floating[Any]]]¶
Get the bounds of this TargetSpace.
- Returns:¶
np.ndarray
- property constraint : ConstraintModel | None¶
Get the constraint model.
- Returns:¶
ConstraintModel
- property constraint_values : ndarray[Any, dtype[floating[Any]]]¶
Get the constraint values registered to this TargetSpace.
- Returns:¶
np.ndarray
- property mask : ndarray[Any, dtype[bool]]¶
Return a boolean array of valid points.
Points are valid if they satisfy both the constraint and boundary conditions.
- Returns:¶
np.ndarray
- max() dict[str, Any] | None ¶
Get maximum target value found and corresponding parameters.
If there is a constraint present, the maximum value that fulfills the constraint within the parameter bounds is returned.
- property params : ndarray[Any, dtype[floating[Any]]]¶
Get the parameter values registered to this TargetSpace.
- Returns:¶
np.ndarray
- params_to_array(params: Mapping[str, float]) ndarray[Any, dtype[floating[Any]]] ¶
Convert a dict representation of parameters into an array version.
- probe(params: Mapping[str, float] | Sequence[float] | ndarray[Any, dtype[floating[Any]]]) float | tuple[float, float | ndarray[Any, dtype[floating[Any]]]] ¶
Evaluate the target function on a point and register the result.
Notes
If params has been previously seen and duplicate points are not allowed, returns a cached value of result.
- Parameters:¶
- params : np.ndarray¶
a single point, with len(x) == self.dim
- Return type:¶
float
|tuple
[float
,float
|ndarray
[Any
,dtype
[floating
[Any
]]]]- Returns:¶
result (float | Tuple(float, float)) – target function value, or Tuple(target function value, constraint value)
Example
>>> target_func = lambda p1, p2: p1 + p2 >>> pbounds = {"p1": (0, 1), "p2": (1, 100)} >>> space = TargetSpace(target_func, pbounds) >>> space.probe([1, 5]) >>> assert self.max()["target"] == 6 >>> assert self.max()["params"] == {"p1": 1.0, "p2": 5.0}
- random_sample() ndarray[Any, dtype[floating[Any]]] ¶
Sample a random point from within the bounds of the space.
- Return type:¶
- Returns:¶
data (ndarray) – [1 x dim] array with dimensions corresponding to self._keys
Examples
>>> target_func = lambda p1, p2: p1 + p2 >>> pbounds = {"p1": (0, 1), "p2": (1, 100)} >>> space = TargetSpace(target_func, pbounds, random_state=0) >>> space.random_sample() array([[ 0.54488318, 55.33253689]])
-
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 ¶ Append a point and its target value to the known data.
Notes
runs in amortized constant time
Examples
>>> target_func = lambda p1, p2: p1 + p2 >>> pbounds = {"p1": (0, 1), "p2": (1, 100)} >>> space = TargetSpace(target_func, pbounds) >>> len(space) 0 >>> x = np.array([0, 0]) >>> y = 1 >>> space.register(x, y) >>> len(space) 1
- res() list[dict[str, Any]] ¶
Get all target values and constraint fulfillment for all parameters.
- Return type:¶
- Returns:¶
res (list) – A list of dictionaries with the keys ‘target’, ‘params’, and ‘constraint’. The value of ‘target’ is the target value, the value of ‘params’ is a dictionary with the parameter names as keys and the parameter values as values, and the value of ‘constraint’ is the constraint fulfillment.
Notes
Does not report if points are within the bounds of the parameter space.