optional#

skrub.optional(value, *, name=None, default=value)[source]#

A choice between value and None.

When a learner is fitted without hyperparameter tuning, the outcome of this choice is value. Pass default=None to make None the default outcome.

Parameters:
valueobject

The outcome (when None is not chosen).

namestr, optional (default=None)

If not None, name is used when displaying search results and can also be used to override the choice’s value by setting it in the environment containing a learner’s inputs.

defaultNoneType, optional

An optional is a choice between the provided value and None. Normally, the default outcome when a learner is used without hyperparameter tuning is the provided value. Pass default=None to make the alternative outcome, None, the default. None is the only allowed value for this parameter.

Returns:
Choice

An object representing this choice, which can be used in a skrub learner.

Examples

optional is useful for optional steps in a DataOps plan, or a learner. If we want to try a learner with or without dimensionality reduction, we can add a step such as:

>>> from sklearn.decomposition import PCA
>>> from skrub import optional
>>> optional(PCA(), name='use dim reduction')
optional(PCA(), name='use dim reduction')

The constructed parameter grid will include a branch of the plan with the the PCA and one without:

>>> print(
... optional(PCA(), name='dim reduction').as_data_op().skb.describe_param_grid()
... )
- dim reduction: [PCA(), None]

When a learner that contains an optional step is used without hyperparameter tuning, the default outcome is the provided value.

>>> print(optional(PCA()).default())
PCA()

This can be overridden by passing default=None:

>>> print(optional(PCA(), default=None).default())
None