bayes_opt.target_space.TargetSpace
¶
-
class bayes_opt.target_space.TargetSpace(target_func, pbounds, constraint=
None
, random_state=None
, allow_duplicate_points=False
)¶ Holds the param-space coordinates (X) and target values (Y).
Allows for constant-time appends.
- Parameters:¶
- target_func : function¶
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.
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}
- property constraint_values¶
Get the constraint values registered to this TargetSpace.
- Returns:¶
np.ndarray
- property mask¶
Return a boolean array of valid points.
Points are valid if they satisfy both the constraint and boundary conditions.
- Returns:¶
np.ndarray
- max()¶
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.
- Returns:¶
res (dict) – A dictionary with the keys ‘target’ and ‘params’. The value of ‘target’ is the maximum target value, and the value of ‘params’ is a dictionary with the parameter names as keys and the parameter values as values.
- probe(params)¶
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
- 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()¶
Sample a random point from within the bounds of the space.
- 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([[ 55.33253689, 0.54488318]])
-
register(params, target, constraint_value=
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()¶
Get all target values and constraint fulfillment for all parameters.
- 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.
- set_bounds(new_bounds)¶
Change the lower and upper search bounds.