optional#

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

A choice between value and None.

When a pipeline is used 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 pipeline’s inputs.

defaultNoneType, optional

An optional is a choice between the provided value and None. Normally, the default outcome when a pipeline 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 pipeline.

Examples

optional is useful for optional steps in a pipeline. If we want to try our pipeline 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 version of the pipeline with the PCA and one without:

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

When a pipeline containing 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