pyntbci.transformers.CCA

class pyntbci.transformers.CCA(n_components: int, gamma_x: float | list[float] | ndarray[Any, dtype[_ScalarType_co]] | None = None, gamma_y: float | list[float] | ndarray[Any, dtype[_ScalarType_co]] | None = None, estimator_x: BaseEstimator | None = None, estimator_y: BaseEstimator | None = None, running: bool = False, alpha_x: float | None = None, alpha_y: float | None = None)[source]

Canonical correlation analysis (CCA). Maximizes the correlation between two variables in their projected spaces. Here, CCA is implemented as the SVD of (cross)covariance matrices [1].

Parameters:
  • n_components (int) – The number of CCA components to use.

  • gamma_x (float | list[float] | NDArray (default: None)) – Regularization on the covariance matrix for CCA for all or each individual parameter along n_features_x. If None, no regularization is applied. The gamma_x ranges from 0 (no regularization) to 1 (full regularization).

  • gamma_y (float | list[float] | NDArray (default: None)) – Regularization on the covariance matrix for CCA for all or each individual parameter along n_features_y. If None, no regularization is applied. The gamma_y ranges from 0 (no regularization) to 1 (full regularization).

  • estimator_x (BaseEstimator (Default: None)) – A BaseEstimator object that estimates a covariance matrix for X using a fit method. If None, a custom implementation of the empirical covariance is used.

  • estimator_y (BaseEstimator (Default: None)) – A BaseEstimator object that estimates a covariance matrix for Y using a fit method. If None, a custom implementation of the empirical covariance is used.

  • running (bool (default: False)) – If False, the CCA is instantaneous, only fit to the current data. If True, the CCA is incremental and keeps track of previous data to update a running average and covariance for the CCA.

  • alpha_x (float (Default: None)) – Amount of variance to retain in computing the inverse of the covariance matrix of X. If None, all variance.

  • alpha_y (float (Default: None)) – Amount of variance to retain in computing the inverse of the covariance matrix of Y. If None, all variance.

w_x_

The weight vector to project X of shape (n_features_x, n_components).

Type:

NDArray

w_y_

The weight vector to project Y of shape (n_features_y, n_components).

Type:

NDArray

n_x_

The number of samples used to estimate avg_x_ and cov_x.

Type:

int

avg_x_

The (running) average of X of shape (n_features_x).

Type:

NDArray

cov_x_

The (running) covariance of X of shape (n_features_x, n_features_x).

Type:

NDArray

n_y_

The number of samples used to estimate avg_y_ and cov_y.

Type:

int

avg_y_

The (running) average of Y of shape (n_features_y).

Type:

NDArray

cov_y_

The (running) covariance of Y of shape (n_features_y, n_features_y).

Type:

NDArray

n_xy_

The number of samples used to estimate avg_xy_ and cov_xy.

Type:

int

avg_xy_

The (running) average of concat(X, Y) of shape (n_features_x + n_features_y).

Type:

NDArray

cov_xy_

The (running) cross-covariance of X and Y of shape (n_features_x, n_features_y).

Type:

NDArray

References

fit(X: ndarray[Any, dtype[_ScalarType_co]], Y: ndarray[Any, dtype[_ScalarType_co]]) TransformerMixin[source]

Fit the CCA in one of 3 ways: (1) X (data) is 3D and y (labels) is 1D, (2) X (data) is 3D and Y (data) is 3D, or (3) X (data) is 2D and Y (data) is 2D.

Parameters:
  • X (NDArray) – Data matrix of shape (n_trials, n_features_x, n_samples) or (n_samples, n_features_x).

  • Y (NDArray) – Data matrix of shape (n_trials, n_features_y, n_samples) or (n_samples, n_features_y), or label vector of shape (n_trials,).

Returns:

self – Returns the instance itself.

Return type:

TransformerMixin

transform(X: ndarray[Any, dtype[_ScalarType_co]] = None, Y: ndarray[Any, dtype[_ScalarType_co]] = None) tuple[ndarray[Any, dtype[_ScalarType_co]], ndarray[Any, dtype[_ScalarType_co]]][source]

Transform the data matrix from feature space to component space. Note, works with both 2D and 3D data, and can operate on both X and Y if both are not None, or on each separately if the other is None.

Parameters:
  • X (NDArray (default: None)) – Data matrix of shape (n_samples, n_features_x) or (n_trials, n_features_x, n_samples). If None, only performs projection of Y.

  • Y (NDArray (default: None)) – Data matrix of shape (n_samples, n_features_y) or (n_trials, n_features_y, n_samples). If None, only performs projection of X.

Returns:

  • X (NDArray) – Projected data matrix of shape (n_samples, n_components) or (n_trials, n_components, n_samples). None if the input was None.

  • Y (NDArray) – Projected data matrix of shape (n_samples, n_components) or (n_trials, n_components, n_samples). None if the input was None.