pyriemann.transfer.TLRotate¶
- class pyriemann.transfer.TLRotate(target_domain, weights=None, metric='euclid', n_jobs=1, expl_var=0.999, n_components=1, n_clusters=3)¶
Rotation for transfer learning.
For inputs in matrix manifold, it rotates the matrices from each source domain so to match its class means with those from the target domain. The loss function for this matching is described in [1] and the optimization procedure for minimizing it in [2].
For inputs in tangent space, it rotates the tangent vectors from each source domain so to match its class means with those from the target domain [3].
Note
The inputs from each domain must have been centered to the before calculating the rotation.
Note
Using .fit() and then .transform() will give different results than .fit_transform(). In fact, .fit_transform() should be applied on the training set (target and source domains), and .transform() on the target domain of the test set.
- Parameters:
- target_domainstr
Domain to consider as target.
- weightsNone | ndarray, shape (n_classes,), default=None
For inputs in manifold, weights to assign for each class. If None, it uses equal weights.
- metric{“euclid”, “riemann”}, default=”euclid”
For inputs in manifold, distance to minimize between class means.
- n_jobsint, default=1
For inputs in manifold, the number of jobs to use for the computation. This works by computing the rotation matrix for each source domain in parallel. If -1 all CPUs are used.
- expl_varfloat, default=0.999
For inputs in tangent space, dimension reduction applied to the cross product matrix during Procrustes analysis. If float in (0,1], percentage of variance that needs to be explained. Else, number of components.
- n_componentsint | “max”, default=1
For inputs in tangent space, parameter n_components used in sklearn.decomposition.PCA. If int, number of components to keep in PCA. If “max”, all components are kept.
- n_clustersint, default=3
For inputs in tangent space, number of clusters used to split data.
- Attributes:
- rotations_dict
Dictionary with key=domain_name and value=domain_rotation_matrix.
See also
Notes
Added in version 0.4.
Changed in version 0.8: Added support for tangent space rotation.
References
[1]Riemannian Procrustes analysis: transfer learning for brain-computer interfaces PLC Rodrigues et al, IEEE Transactions on Biomedical Engineering, vol. 66, no. 8, pp. 2390-2401, December, 2018
[2]An introduction to optimization on smooth manifolds N. Boumal. To appear with Cambridge University Press. June, 2022
[3]Tangent space alignment: Transfer learning for brain-computer interface A. Bleuzé, J. Mattout and M. Congedo, Frontiers in Human Neuroscience, 2022
- __init__(target_domain, weights=None, metric='euclid', n_jobs=1, expl_var=0.999, n_components=1, n_clusters=3)¶
Init
- fit(X, y_enc, sample_weight=None)¶
Fit TLRotate.
It calculates the rotations matrices to transform each source domain into the target domain.
- Parameters:
- Xndarray, shape (n_matrices, n_channels, n_channels) or shape (n_vectors, n_ts)
Set of SPD matrices or tangent vectors.
- y_encndarray, shape (n_matrices,) or shape (n_vectors,)
Extended labels for each matrix or vector.
- sample_weightNone | ndarray, shape (n_matrices,) or shape (n_vectors,), default=None
Weights for each matrix or vector. If None, it uses equal weights.
- Returns:
- selfTLRotate instance
The TLRotate instance.
- fit_transform(X, y_enc, sample_weight=None)¶
Fit TLRotate and then rotate each source domain to target domain.
It calculates and applies the rotation matrix for matching each source domain to the target domain.
Note
This method is designed for using at training time. The output for .fit_transform() will be different than using .fit() and .transform() separately.
- Parameters:
- Xndarray, shape (n_matrices, n_channels, n_channels) or shape (n_vectors, n_ts)
Set of SPD matrices or tangent vectors.
- y_encndarray, shape (n_matrices,) or shape (n_vectors,)
Extended labels for each matrix or vector.
- sample_weightNone | ndarray, shape (n_matrices,) or shape (n_vectors,), default=None
Weights for each matrix or vector. If None, it uses equal weights.
- Returns:
- X_newndarray, shape (n_matrices, n_classes)
Set of rotated SPD matrices or tangent vectors to target domain.
- 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.
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$', y_enc: bool | None | str = '$UNCHANGED$') TLRotate ¶
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if 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.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter infit
.- y_encstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
y_enc
parameter infit
.
- 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.
- transform(X)¶
Rotate in the target domain, ie do nothing.
Note
This method is designed for using at test time on target data. No transformation is applied since rotations are done from source to target domain.
- Parameters:
- Xndarray, shape (n_matrices, n_channels, n_channels) or shape (n_vectors, n_ts)
Set of SPD matrices or tangent vectors.
- Returns:
- Xndarray, shape (n_matrices, n_channels, n_channels) or shape (n_vectors, n_ts)
Same data as in the input.
Examples using pyriemann.transfer.TLRotate
¶

Data transformations in the Riemannian Procrustes Analysis