pyriemann.preprocessing.Whitening

class pyriemann.preprocessing.Whitening(metric='euclid', dim_red=None, verbose=False)

Whitening, and optional unsupervised dimension reduction.

Implementation of the whitening, and an optional unsupervised dimension reduction, with SPD matrices as inputs.

Parameters:
metricstr, default=”euclid”

Metric for the estimation of mean matrix used for whitening and dimension reduction. For the list of supported metrics, see pyriemann.utils.mean.mean_covariance().

dim_redNone | dict, default=None
If None :

no dimension reduction during whitening.

If {'n_components': val} :

dimension reduction defining the number of components; val must be an integer superior to 1.

If {'expl_var': val} :

dimension reduction selecting the number of components such that the amount of variance that needs to be explained is greater than the percentage specified by val. val must be a float in (0,1], typically 0.99.

If {'max_cond': val} :

dimension reduction selecting the number of components such that the condition number of the mean matrix is lower than val. This threshold has a physiological interpretation, because it can be viewed as the ratio between the power of the strongest component (usually, eye-blink source) and the power of the lowest component you don’t want to keep (acquisition sensor noise). val must be a float strictly superior to 1, typically 100.

verbosebool, default=False

Verbose flag.

Attributes:
n_components_int

If fit, the number of components after dimension reduction.

filters_ndarray, shape (n_channels, n_components_)

If fit, the spatial filters to whiten SPD matrices.

inv_filters_ndarray, shape (n_components_, n_channels)

If fit, the spatial filters to unwhiten SPD matrices.

Notes

Added in version 0.2.7.

__init__(metric='euclid', dim_red=None, verbose=False)

Init.

fit(X, y=None, sample_weight=None)

Train whitening spatial filters.

Parameters:
Xndarray, shape (n_matrices, n_channels, n_channels)

Set of SPD matrices.

yNone

Ignored as unsupervised.

sample_weightNone | ndarray, shape (n_matrices,), default=None

Weight of each matrix, to compute the weighted mean matrix used for whitening and dimension reduction. If None, it uses equal weights.

Returns:
selfWhitening instance

The Whitening instance.

fit_transform(X, y=None, sample_weight=None)

Fit and transform in a single function.

Parameters:
Xndarray, shape (n_matrices, n_channels, n_channels)

Set of SPD matrices.

yNone

Ignored as unsupervised.

sample_weightNone | ndarray, shape (n_matrices,), default=None

Weight of each matrix, to compute the weighted mean matrix used for whitening and dimension reduction. If None, it uses equal weights.

Returns:
X_newndarray, shape (n_matrices, n_components_, n_components_)

Set of whitened, and optionally reduced, SPD matrices.

get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

inverse_transform(X)

Apply inverse whitening spatial filters.

Parameters:
Xndarray, shape (n_matrices, n_components_, n_components_)

Set of whitened, and optionally reduced, SPD matrices.

Returns:
X_newndarray, shape (n_matrices, n_channels, n_channels)

Set of unwhitened, and optionally unreduced, SPD matrices.

partial_fit(X, y=None, *, sample_weight=None, alpha=None)

Partially fit whitening spatial filters.

Parameters:
Xndarray, shape (n_matrices, n_channels, n_channels)

Set of SPD matrices.

yNone

Ignored as unsupervised.

sample_weightNone | ndarray, shape (n_matrices,), default=None

Weights for each matrix. If None, it uses equal weights.

alphafloat | None, default=None

Update rate in [0, 1] for the mean: 0 for no update, 1 for full update. If None, alpha is defined as n_matrices divided by the number of matrices that have been already used for fit.

Returns:
selfWhitening instance

The Whitening instance.

Notes

Added in version 0.7.

set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') Whitening

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in fit.

Returns:
selfobject

The updated object.

set_output(*, transform=None)

Set output container.

See Introducing the set_output API for an example on how to use the API.

Parameters:
transform{“default”, “pandas”, “polars”}, default=None

Configure output of transform and fit_transform.

  • “default”: Default output format of a transformer

  • “pandas”: DataFrame output

  • “polars”: Polars output

  • None: Transform configuration is unchanged

Added in version 1.4: “polars” option was added.

Returns:
selfestimator instance

Estimator instance.

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

set_partial_fit_request(*, alpha: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') Whitening

Configure whether metadata should be requested to be passed to the partial_fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to partial_fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to partial_fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
alphastr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for alpha parameter in partial_fit.

sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in partial_fit.

Returns:
selfobject

The updated object.

transform(X)

Apply whitening spatial filters.

Parameters:
Xndarray, shape (n_matrices, n_channels, n_channels)

Set of SPD matrices.

Returns:
X_newndarray, shape (n_matrices, n_components_, n_components_)

Set of whitened, and optionally reduced, SPD matrices.