coco_pipe.transforms.LeaceEraser#

class coco_pipe.transforms.LeaceEraser(*, eta2=0.5, max_rank=None)#

Bases: SubjectAlignment

Erase the directions in which subjects differ most (rank-limited LEACE).

Each direction v of the feature space has a subject eta^2, the share of its variance that lies between subjects: the solutions of S_b v = eta^2 S_t v, with S_t the total covariance and S_b the between-subject covariance of the subject means. The eraser removes the r directions of largest eta^2 with LEACE’s oblique projector P = I - S_t V_r V_r^T (V_r^T S_t V_r = I): no linear function of the erased data then varies along them, and the rest of the space is kept. Erasing every direction in which subject means differ (full LEACE) leaves nothing once there are more subjects than features.

r is the number of directions with eta^2 of at least eta2, at most max_rank: eta2 leaves alone the directions that are mostly within-subject variance, max_rank bounds how much is erased, and eta2=0 with max_rank erases exactly that many.

With condition in fit(), S_b is taken from the subject x condition cell means around their condition’s mean, so a subject with more windows of one condition does not count that as identity, and a difference between conditions shared by the subjects is kept.

The projector is fixed after fit(), so it applies to subjects it has not seen, without their groups or conditions.

Parameters:
  • eta2 (float, default=0.5) – Erase the directions whose subject eta^2 is at least this.

  • max_rank (int or None, default=None) – Erase at most this many of them, the largest eta^2 first.

Variables:
  • rank (int) – Number of directions erased.

  • eta2 (ndarray of shape (n_features,)) – Subject eta^2 of every direction, largest first.

  • components (ndarray of shape (n_features, n_features)) – Every direction as a row, in the order of eta2_; the first rank_ are erased. (X - mu_) @ components_.T is each row’s score on them, with unit total variance.

  • n_subjects (int) – Number of subjects fitted.

References

Belrose, N. et al. (2023). LEACE: Perfect linear concept erasure in closed form. NeurIPS.

name = 'leace'#
label = 'Leace'#
fold_local: bool = True#
fit(X, y=None, groups=None, condition=None)#

Find the directions to erase.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – The vectors, e.g. one embedding per window.

  • y (None) – Ignored.

  • groups (array-like of shape (n_samples,)) – The subject of each row.

  • condition (array-like of shape (n_samples,), optional) – The condition of each row, e.g. eyes open or closed.

Raises:

ValueError – If the eraser would remove 95% of the feature directions or more.

transform(X, groups=None)#
fit_transform(X, y=None, groups=None, condition=None, **fit_params)#

Fit the eraser and erase the same observations.

fingerprint()#
Return type:

str

get_metadata_routing()#

Ask for each row’s subject in fit and transform, and for its condition in fit when fit takes one.

get_params(deep=True)#

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

set_fit_request(*, condition='$UNCHANGED$', groups='$UNCHANGED$')#

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:
  • condition (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for condition parameter in fit.

  • groups (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for groups parameter in fit.

  • self (LeaceEraser)

Returns:

self – The updated object.

Return type:

object

set_output(*, transform=None)#

Set output container.

Refer to the user guide for more details and 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:

self – Estimator instance.

Return type:

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:

**params (dict) – Estimator parameters.

Returns:

self – Estimator instance.

Return type:

estimator instance

set_transform_request(*, groups='$UNCHANGED$')#

Configure whether metadata should be requested to be passed to the transform 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 transform 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 transform.

  • 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:
  • groups (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for groups parameter in transform.

  • self (LeaceEraser)

Returns:

self – The updated object.

Return type:

object