coco_pipe.transforms.LeaceEraser#
- class coco_pipe.transforms.LeaceEraser(*, eta2=0.5, max_rank=None)#
Bases:
SubjectAlignmentErase the directions in which subjects differ most (rank-limited LEACE).
Each direction
vof the feature space has a subject eta^2, the share of its variance that lies between subjects: the solutions ofS_b v = eta^2 S_t v, withS_tthe total covariance andS_bthe between-subject covariance of the subject means. The eraser removes therdirections of largest eta^2 with LEACE’s oblique projectorP = 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.ris the number of directions with eta^2 of at leasteta2, at mostmax_rank:eta2leaves alone the directions that are mostly within-subject variance,max_rankbounds how much is erased, andeta2=0withmax_rankerases exactly that many.With
conditioninfit(),S_bis 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:
- 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 firstrank_are erased.(X - mu_) @ components_.Tis 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'#
- 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.
- get_metadata_routing()#
Ask for each row’s subject in
fitandtransform, and for its condition infitwhenfittakes one.
- get_params(deep=True)#
Get parameters for this estimator.
- set_fit_request(*, condition='$UNCHANGED$', groups='$UNCHANGED$')#
Configure whether metadata should be requested to be passed to the
fitmethod.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(seesklearn.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 tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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
conditionparameter infit.groups (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
groupsparameter infit.self (LeaceEraser)
- Returns:
self – The updated object.
- Return type:
- 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
transformmethod.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(seesklearn.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 totransformif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it totransform.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
groupsparameter intransform.self (LeaceEraser)
- Returns:
self – The updated object.
- Return type: