mindfoundry.optaas.client.sklearn_pipelines package

Subpackages

Submodules

mindfoundry.optaas.client.sklearn_pipelines.mixin module

class mindfoundry.optaas.client.sklearn_pipelines.mixin.EstimatorChoice(*estimators: Union[BaseEstimator, Optimizable], optional: bool = False)[source]

Bases: Optimizable

Allows OPTaaS to choose one of many estimators for a step in a pipeline.

Parameters
  • *estimators (Estimator) – Estimators from which to choose.

  • optional (bool) – Whether this will be an optional step (defaults to False).

make_all_parameters_constraints_and_prior_means(estimator_name: str, id_prefix: str, **kwargs) Tuple[List[Parameter], List[Constraint], List[PriorMeanExpression]][source]

Returns all parameters, constraints, and prior Means for multiple estimators in a group or choice.

class mindfoundry.optaas.client.sklearn_pipelines.mixin.OptimizableBaseEstimator[source]

Bases: BaseEstimator, Optimizable, ABC

Mixin that allows an estimator to be optimized by OPTaaS. Subclasses must implement make_parameters_and_constraints.

get_required_kwarg(kwargs, arg_name: str) Any[source]

Returns value of a kwarg required to optimize this estimator. Raises error if argument not set.

Parameters
  • kwargs – Arguments taken from make_parameters_and_constraints().

  • arg_name (str) – Name of the required argument.

Returns

Value of argument.

Raises

.MissingArgumentError

make_all_parameters_constraints_and_prior_means(estimator_name: str, id_prefix: str, **kwargs) Tuple[List[Parameter], List[Constraint], List[PriorMeanExpression]][source]

Returns all parameters, constraints, and prior Means for multiple estimators in a group or choice.

abstract make_parameters_constraints_and_prior_means(sk: SklearnParameterMaker, **kwargs) Tuple[List[Parameter], List[Constraint], List[PriorMeanExpression]][source]

Abstract method that should generate the Parameters and Constraints required to optimize a sklearn estimator.

When implementing this method, make sure to use the SklearnParameterMaker sk to create parameters e.g. call sk.IntParameter(…) instead of IntParameter(…).

If the parameter you want to optimize can take values of different types, use a ChoiceParameter. For an example, see n_components in PCA. If the parameter value needs to be a list or array, use a GroupParameter. For an example, see weights in VotingClassifier.

Parameters
  • sk (SklearnParameterMaker) – Allows you to create parameters with the correct names and defaults.

  • kwargs – Additional arguments required to optimize certain estimators, e.g. feature_count (number of features in your data set, required to optimize PCA)

Returns

A tuple of 3 lists (Parameters, Constraints, PriorMeans)

Raises

.MissingArgumentError

class mindfoundry.optaas.client.sklearn_pipelines.mixin.OptimizablePipeline(estimators: List[Tuple[str, Union[BaseEstimator, Optimizable]]], optional: bool = False)[source]

Bases: Optimizable

A pipeline-like object to which will be used to generate parameters and constraints for optimization.

Parameters
  • estimators (List[EstimatorTuple]) –

    List of (name, estimator) tuples as you would provide when creating a sklearn Pipeline. An estimator can be:

  • optional (bool) – Whether this will be an optional step (defaults to False).

make_all_parameters_constraints_and_prior_means(estimator_name: str, id_prefix: str, **kwargs) Tuple[List[Parameter], List[Constraint], List[PriorMeanExpression]][source]

Returns all parameters, constraints, and prior Means for multiple estimators in a group or choice.

class mindfoundry.optaas.client.sklearn_pipelines.mixin.OptionalStepMixin[source]

Bases: OptimizableBaseEstimator

Mixin that allows an estimator to be optional, i.e. it may be omitted from a Configuration generated by OPTaaS.

Example

class MyEstimator(OptionalStepMixin):

Your estimator can define make_parameters_and_constraints if you wish to optimize its parameters, or you can leave it undefined and use the default provided below.

make_parameters_constraints_and_prior_means(sk: SklearnParameterMaker, **kwargs) Tuple[List[Parameter], List[Constraint], List[PriorMeanExpression]][source]

A default implementation for when you need an optional estimator without optimizing any of its parameters.

mindfoundry.optaas.client.sklearn_pipelines.mixin.choice(*estimators: Union[BaseEstimator, Optimizable]) EstimatorChoice[source]

Convenience method for creating a choice of estimators in a pipeline.

mindfoundry.optaas.client.sklearn_pipelines.mixin.optional_choice(*estimators: Union[BaseEstimator, Optimizable]) EstimatorChoice[source]

Convenience method for creating a choice of estimators as an optional step in a pipeline.

mindfoundry.optaas.client.sklearn_pipelines.mixin.optional_step(estimator: Union[BaseEstimator, Optimizable]) OptionalStepMixin[source]

Wrapper method to easily make an estimator optional in an OPTaaS SklearnTask.

The OptionalStepMixin class will be added to the estimator object’s base classes (only this instance will be affected, not the entire class).

Example

create_sklearn_task(estimators=[ (‘my_optional_step’, optional_step(MyEstimator())) ])

mindfoundry.optaas.client.sklearn_pipelines.parameter_maker module

class mindfoundry.optaas.client.sklearn_pipelines.parameter_maker.SklearnParameterMaker(estimator_id: str, estimator: BaseEstimator)[source]

Bases: object

Creates Parameters with the correct names and default values for optimizing a sklearn Pipeline

Convenience methods are provided for each Parameter subclass, so that you can call sk.IntParameter(…) instead of IntParameter(…).

BoolParameter(name: str, optional: Optional[bool] = None) BoolParameter[source]
CategoricalParameter(name: str, values: Sequence[Union[str, int, float, bool]], optional: Optional[bool] = None) CategoricalParameter[source]
ChoiceParameter(name: str, choices: List[Parameter], optional: Optional[bool] = None) ChoiceParameter[source]
ConstantParameter(name: str, value: Union[str, int, float, bool], optional: Optional[bool] = None) ConstantParameter[source]
DictParameter(name: str, items: List[Parameter], optional: Optional[bool] = None) GroupParameter[source]

Creates a parameter whose value will be passed into an estimator as a dict.

FloatOrAuto(name: str, minimum: float, maximum: float, distribution: Optional[Distribution] = None, optional: Optional[bool] = None) ChoiceParameter[source]

Creates a choice between a FloatParameter and the string ‘auto’.

FloatOrCategorical(name: str, minimum: float, maximum: float, categories: List[str], distribution: Optional[Distribution] = None, optional: Optional[bool] = None) ChoiceParameter[source]

Creates a choice between a FloatParameter and a CategoricalParameter.

FloatParameter(name: str, minimum: float, maximum: float, distribution: Optional[Distribution] = None, optional: Optional[bool] = None) FloatParameter[source]
GroupParameter(name: str, items: List[Parameter], optional: Optional[bool] = None) GroupParameter[source]
IntOrAuto(name: str, minimum: int, maximum: int, distribution: Optional[Distribution] = None, optional: Optional[bool] = None) ChoiceParameter[source]

Creates a choice between an IntParameter and the string ‘auto’.

IntOrCategorical(name: str, minimum: int, maximum: int, categories: List[str], distribution: Optional[Distribution] = None, optional: Optional[bool] = None) ChoiceParameter[source]

Creates a choice between an IntParameter and a CategoricalParameter.

IntParameter(name: str, minimum: int, maximum: int, distribution: Optional[Distribution] = None, optional: Optional[bool] = None) IntParameter[source]
SubsetParameter(name: str, values: List[Union[str, int, float, bool]], optional: Optional[bool] = None) SubsetParameter[source]
make_parameter(parameter_type: Callable[[...], T], name: str, **kwargs) T[source]

Creates a parameter so as to facilitate the generation of a sklearn Pipeline from a Configuration.

Parameters
  • parameter_type (Callable[..., T]) – The specific Parameter subclass of the parameter you want to create, e.g. IntParameter.

  • name (str) – Parameter name, should match the name expected by the estimator’s set_params method exactly.

  • kwargs – Any additional arguments for the parameter constructor, e.g. minimum, maximum, choices etc. Do not include a value for the id and default arguments, because it will be overwritten. The id will be generated from the parameter name (any spaces will be replaced by underscores) and prefixed with the estimator name. The default will be taken from estimator.get_params(), i.e. it should be set in the estimator constructor.

mindfoundry.optaas.client.sklearn_pipelines.sklearn_task module

class mindfoundry.optaas.client.sklearn_pipelines.sklearn_task.SklearnTask(task: Task, estimators: List[Tuple[str, Union[BaseEstimator, Optimizable]]])[source]

Bases: Task

A Task that can convert a Configuration into a sklearn Pipeline

make_pipeline(configuration: Configuration) Pipeline[source]

Creates a sklearn Pipeline and sets its parameters based on the provided Configuration

run(scoring_function: Callable[[Pipeline], Union[float, Dict[str, float], Tuple[float, float], Tuple[Dict[str, float], Dict[str, float]]]], max_iterations: int, score_threshold: Optional[Union[float, Dict[str, float]]] = None, logging_level: Literal['DEBUG', 'INFO', 'WARNING', 'ERROR'] = 'INFO') Union[StoredResult, List[StoredResult]][source]

Run this task, using the provided scoring function to calculate the score for each configuration.

Parameters
  • scoring_function (Callable[[Pipeline], ScoringFunctionOutput]) – Function that takes a sklearn Pipeline as input and returns a score or a tuple of (score, variance).

  • max_iterations (int) – Max number of iterations to run, i.e. number of results to record before stopping.

  • score_threshold (float, optional) – Stop running the task when the score is equal to or better than this value.

  • logging_level (Literal["DEBUG", "INFO", "WARNING", "ERROR"], optional, defaults to "INFO") – Set the logging level.

Returns

The best recorded Result with the Pipeline that was used to achieve it. For multi-objective tasks, the set of Pareto front Results will be returned instead.

Raises

.OPTaaSError

mindfoundry.optaas.client.sklearn_pipelines.utils module

exception mindfoundry.optaas.client.sklearn_pipelines.utils.MissingArgumentError(required_arg: str, estimator)[source]

Bases: ValueError

Raised when a required argument is missing from kwargs in OPTaaSClient.create_sklearn_task()

class mindfoundry.optaas.client.sklearn_pipelines.utils.Optimizable[source]

Bases: ABC

Superclass for all optimizable steps.

abstract make_all_parameters_constraints_and_prior_means(estimator_name: str, id_prefix: str, **kwargs) Tuple[List[Parameter], List[Constraint], List[PriorMeanExpression]][source]

Returns all parameters, constraints, and prior Means for multiple estimators in a group or choice.

Module contents