SubspaceDiscrete¶
- class baybe.searchspace.discrete.SubspaceDiscrete[source]¶
Bases:
SerialMixin
Class for managing discrete subspaces.
Builds the subspace from parameter definitions and optional constraints, keeps track of search metadata, and provides access to candidate sets and different parameter views.
Public methods
__init__
(parameters, exp_rep[, metadata, ...])Method generated by attrs for class SubspaceDiscrete.
empty
()Create an empty discrete subspace.
estimate_product_space_size
(parameters)Estimate an upper bound for the memory size of a product space.
from_dataframe
(df[, parameters, empty_encoding])Create a discrete subspace with a specified set of configurations.
from_dict
(dictionary)Create an object from its dictionary representation.
from_json
(string)Create an object from its JSON representation.
from_parameter
(parameter)Create a subspace from a single parameter.
from_product
(parameters[, constraints, ...])from_simplex
(max_sum, simplex_parameters[, ...])Efficiently create discrete simplex subspaces.
get_candidates
([...])Return the set of candidate parameter settings that can be tested.
get_parameters_by_name
(names)Return parameters with the specified names.
mark_as_measured
(measurements, ...)Mark the given elements of the space as measured.
to_dict
()Create an object's dictionary representation.
to_json
()Create an object's JSON representation.
Turn the subspace into a search space with no continuous part.
transform
([df, allow_missing, allow_extra, data])Public attributes and properties
The list of parameters of the subspace.
The experimental representation of the subspace.
The metadata.
Flag encoding whether an empty encoding is used.
A list of constraints for restricting the space.
The computational representation of the space.
The minimum and maximum values of the computational representation.
The columns spanning the computational representation.
Return whether this subspace is empty.
Return tuple of parameter names.
- __init__(parameters: Collection[Parameter], exp_rep: DataFrame, metadata: DataFrame = NOTHING, empty_encoding: bool = False, constraints: Sequence = NOTHING, comp_rep: DataFrame = NOTHING)¶
Method generated by attrs for class SubspaceDiscrete.
For details on the parameters, see Public attributes and properties.
- static estimate_product_space_size(parameters: Sequence[DiscreteParameter])[source]¶
Estimate an upper bound for the memory size of a product space.
- Parameters:
parameters (
Sequence
[DiscreteParameter
]) – The parameters spanning the product space.- Return type:
- Returns:
The estimated memory size.
- classmethod from_dataframe(df: DataFrame, parameters: Sequence[DiscreteParameter] | None = None, empty_encoding: bool = False)[source]¶
Create a discrete subspace with a specified set of configurations.
- Parameters:
df (
DataFrame
) – The experimental representation of the search space to be created.parameters (
Optional
[Sequence
[DiscreteParameter
]]) – Optional parameter objects corresponding to the columns in the given dataframe that can be provided to explicitly control parameter attributes. If a match between column name and parameter name is found, the corresponding parameter object is used. If a column has no match in the parameter list, abaybe.parameters.numerical.NumericalDiscreteParameter
is created if possible, or abaybe.parameters.categorical.CategoricalParameter
is used as fallback. For both types, default values are used for their optional arguments. For more details, seebaybe.parameters.utils.get_parameters_from_dataframe()
.empty_encoding (
bool
) – Seebaybe.searchspace.core.SearchSpace.from_product()
.
- Return type:
- Returns:
The created discrete subspace.
- classmethod from_parameter(parameter: DiscreteParameter)[source]¶
Create a subspace from a single parameter.
- Parameters:
parameter (
DiscreteParameter
) – The parameter to span the subspace.- Return type:
- Returns:
The created subspace.
- classmethod from_product(parameters: Sequence[DiscreteParameter], constraints: Sequence[DiscreteConstraint] | None = None, empty_encoding: bool = False)[source]¶
See
baybe.searchspace.core.SearchSpace
.- Return type:
- classmethod from_simplex(max_sum: float, simplex_parameters: Sequence[NumericalDiscreteParameter], product_parameters: Sequence[DiscreteParameter] | None = None, constraints: Sequence[DiscreteConstraint] | None = None, min_nonzero: int = 0, max_nonzero: int | None = None, boundary_only: bool = False, tolerance: float = 1e-06)[source]¶
Efficiently create discrete simplex subspaces.
The same result can be achieved using
baybe.searchspace.discrete.SubspaceDiscrete.from_product()
in combination with appropriate constraints. However, such an approach is inefficient because the Cartesian product involved creates an exponentially large set of candidates, most of which do not satisfy the simplex constraints and must be subsequently be filtered out by the method.By contrast, this method uses a shortcut that removes invalid candidates already during the creation of parameter combinations, resulting in a significantly faster construction.
- Parameters:
max_sum (
float
) – The maximum sum of the parameter values defining the simplex size.simplex_parameters (
Sequence
[NumericalDiscreteParameter
]) – The parameters to be used for the simplex construction.product_parameters (
Optional
[Sequence
[DiscreteParameter
]]) – Optional parameters that enter in form of a Cartesian product.constraints (
Optional
[Sequence
[DiscreteConstraint
]]) – Seebaybe.searchspace.core.SearchSpace
.min_nonzero (
int
) – Optional restriction on the minimum number of nonzero parameter values in the simplex construction.max_nonzero (
Optional
[int
]) – Optional restriction on the maximum number of nonzero parameter values in the simplex construction.boundary_only (
bool
) – Flag determining whether to keep only parameter configurations on the simplex boundary.tolerance (
float
) – Numerical tolerance used to validate the simplex constraint.
- Raises:
ValueError – If the passed simplex parameters are not suitable for a simplex construction.
ValueError – If the passed product parameters are not discrete.
ValueError – If the passed simplex parameters and product parameters are not disjoint.
- Return type:
- Returns:
The created simplex subspace.
Note
The achieved efficiency gains can vary depending on the particular order in which the parameters are passed to this method, as the configuration space is built up incrementally from the parameter sequence.
- get_candidates(allow_repeated_recommendations: bool = False, allow_recommending_already_measured: bool = False, exclude: DataFrame | None = None)[source]¶
Return the set of candidate parameter settings that can be tested.
- Parameters:
allow_repeated_recommendations (
bool
) – IfTrue
, parameter settings that have already been recommended in an earlier iteration are still considered valid candidates. This is relevant, for instance, when an earlier recommended parameter setting has not been measured by the user (for any reason) after the corresponding recommendation was made.allow_recommending_already_measured (
bool
) – IfTrue
, parameters settings for which there are already target values available are still considered as valid candidates.exclude (
Optional
[DataFrame
]) – Points in experimental representation that should be excluded as candidates.
- Return type:
- Returns:
The candidate parameter settings both in experimental and computational representation.
- get_parameters_by_name(names: Sequence[str])[source]¶
Return parameters with the specified names.
- Parameters:
- Return type:
- Returns:
The named parameters.
- mark_as_measured(measurements: DataFrame, numerical_measurements_must_be_within_tolerance: bool)[source]¶
Mark the given elements of the space as measured.
- Parameters:
measurements (
DataFrame
) – A dataframe containing parameter settings that should be marked as measured.numerical_measurements_must_be_within_tolerance (
bool
) – Seebaybe.utils.dataframe.fuzzy_row_match()
.
- Return type:
- to_json()¶
Create an object’s JSON representation.
- Return type:
- Returns:
The JSON representation as a string.
- to_searchspace()[source]¶
Turn the subspace into a search space with no continuous part.
- Return type:
- transform(df: DataFrame | None = None, /, *, allow_missing: bool = False, allow_extra: bool | None = None, data: DataFrame | None = None)[source]¶
See
baybe.searchspace.core.SearchSpace.transform()
.- Return type:
-
comp_rep:
DataFrame
¶ The computational representation of the space. Technically not required but added as an optional initializer argument to allow ingestion from e.g. serialized objects and thereby speed up construction. If not provided, the default hook will derive it from
exp_rep
.
- property comp_rep_bounds: DataFrame¶
The minimum and maximum values of the computational representation.
-
constraints:
tuple
[DiscreteConstraint
,...
]¶ A list of constraints for restricting the space.
-
parameters:
tuple
[DiscreteParameter
,...
]¶ The list of parameters of the subspace.