metabci.brainda.algorithms.decomposition.STDA module¶
The Spatial-Temporal Discriminant Analysis (STDA) algorithm maximizes the discriminability of the projected features between target and non-target classes by alternately and synergistically optimizing the spatial and temporal dimensions of the EEG in order to learn two projection matrices. Using the learned two projection matrices to transform each of the constructed spatial-temporal two-dimensional samples into new one-dimensional samples with significantly lower dimensions effectively improves the covariance matrix parameter estimation and enhances the generalization ability of the learned classifiers under small training sample sets.
author: Jin Han
email: jinhan9165@gmail.com
Created on: 2022-05
- update log:
2023/12/08 by Yin ZiFan, promise010818@gmail.com, update code annotation
- Refer: [1] Zhang, Yu, et al. “Spatial-temporal discriminant analysis for ERP-based brain-computer interface.”
IEEE Transactions on Neural Systems and Rehabilitation Engineering 21.2 (2013): 233-243.
Application: Spatial-Temporal Discriminant Analysis (STDA)
- class metabci.brainda.algorithms.decomposition.STDA.STDA(L: int = 6, max_iter: int = 400, eps: float = 1e-05)[source]¶
Bases:
BaseEstimator,TransformerMixin,ClassifierMixinSpatial-Temporal Discriminant Analysis (STDA). Note that the parameters naming are exactly the same as in the paper for convenient application.
- Parameters:
L (int) – the number of eigenvectors retained for projection matrices.
max_iter (int, default=400) – Max iteration times.
eps (float, default=1e-5, also can be 1e-10.) – Error to guarantee convergence. Error = norm2(W(n) - W(n-1)), see more details in paper[1].
- W1¶
Weight vector. Actually, D1=n_chs.
- Type:
ndarray of shape (D1, self.L)
- W2¶
Weight vector. Actually, D2=n_features.
- Type:
ndarray of shape (D2, self.L)
- iter_times¶
Iteration times of STDA.
- Type:
int
- wf¶
Weight vector of LDA after the raw features are projected by STDA.
- Type:
ndarray of shape (1, L*L)
References
- [1] Zhang, Yu, et al. “Spatial-temporal discriminant analysis for ERP-based brain-computer interface.”
IEEE Transactions on Neural Systems and Rehabilitation Engineering 21.2 (2013): 233-243.
Tip
A example using STDA¶import numpy as np from metabci.brainda.algorithms.decomposition import STDA Xtrain2 = np.random.randint(-10, 10, (100*2, 16, 19)) y2 = np.hstack((np.ones(100, dtype=int), np.ones(100, dtype=int) * -1)) Xtest2 = np.random.randint(-10, 10, (4, 16, 19)) clf3 = STDA() clf3.fit(Xtrain2, y2) z=clf3.transform(Xtest2) print(clf3.transform(Xtest2))
- fit(X, y)[source]¶
Fit Spatial-Temporal Discriminant Analysis (STDA) model.
- Parameters:
X (array-like of shape (n_samples, n_chs, n_features)) – Training data.
y (array-like of shape (n_samples,)) – Target values. {-1, 1} or {0, 1}
- Returns:
self – Fitted estimator (i.e. self.W1, self.W2).
- Return type:
object
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') STDA¶
Request metadata passed to the
scoremethod.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 toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.New 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_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inscore.- Returns:
self – The updated object.
- Return type:
object
- set_transform_request(*, Xtest: bool | None | str = '$UNCHANGED$') STDA¶
Request metadata passed to the
transformmethod.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 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.New 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:
Xtest (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
Xtestparameter intransform.- Returns:
self – The updated object.
- Return type:
object
- metabci.brainda.algorithms.decomposition.STDA.lda_kernel(X1: ndarray, X2: ndarray)[source]¶
Linear Discriminant analysis kernel that is appliable to binary problems.
- Parameters:
X1 (ndarray of shape (n_samples, n_features)) – samples for class 1 (i.e. positive samples)
X2 (ndarray of shape (n_samples, n_features)) – samples for class 2 (i.e. negative samples)
- Returns:
weight_vec (ndarray of shape (1, n_features)) – weight vector.
lda_threshold (float)
Note
- The test samples should be formatted as (n_samples, n_features).
test sample is positive, if W @ test_sample.T > lda_thold. test sample is negative, if W @ test_sample.T <= lda_thold.
- metabci.brainda.algorithms.decomposition.STDA.lda_proba(test_samples: ndarray, weight_vec: ndarray, lda_threshold: float)[source]¶
Calculate decision value.
- Parameters:
test_samples (2-D, (n_samples, n_features)) –
weight_vec (from LDA_kernel.) –
lda_threshold (from LDA_kernel.) –
- Returns:
proba
- Return type:
ndarray of shape (n_samples,)