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}
array_to_params(x)

Convert an array representation of parameters into a dict version.

Parameters:
x : np.ndarray

a single point, with len(x) == self.dim.

Returns:

dict – Representation of the parameters as dictionary.

property bounds

Get the bounds of this TargetSpace.

Returns:

np.ndarray

property constraint

Get the constraint model.

Returns:

ConstraintModel

property constraint_values

Get the constraint values registered to this TargetSpace.

Returns:

np.ndarray

property dim

Get the number of parameter names.

Returns:

int

property empty

Check if anything has been registered.

Returns:

bool

property keys

Get the keys (or parameter names).

Returns:

list of str

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.

property params

Get the parameter values registered to this TargetSpace.

Returns:

np.ndarray

params_to_array(params)

Convert a dict representation of parameters into an array version.

Parameters:
params : dict

a single point, with len(x) == self.dim.

Returns:

np.ndarray – Representation of the parameters as an array.

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.

Parameters:
params : np.ndarray

a single point, with len(x) == self.dim.

target : float

target function value

constraint_value : float or None

Constraint function value

Raises:

NotUniqueError: – if the point is not unique

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.

Parameters:
new_bounds : dict

A dictionary with the parameter name and its new bounds

property target

Get the target function values registered to this TargetSpace.

Returns:

np.ndarray