pyeeg.preprocess.Whitener

class pyeeg.preprocess.Whitener(axis=0, zca=False, bias=True)

A data whitener (via either PCA or ZCA).

Whitening linearly transforms the data so that its covariance becomes the identity matrix, decorrelating the channels and rescaling them to unit variance. Two standard whitening transforms are supported:

  • PCA whitening (zca=False): W = diag(1/sqrt(eigval)) @ eigvec.T.

  • ZCA whitening (zca=True): W = eigvec @ diag(1/sqrt(eigval)) @ eigvec.T, which additionally rotates the whitened data back to the original channel space (also called Mahalanobis or zero-phase whitening).

The transform is (X - mu) @ W.T; the mean mu is stored during fit() (via demean()) so that transform() and inverse() can be applied consistently to new data.

Parameters:
  • axis (int) – Axis along which to compute the mean and covariance. Default is 0 (samples first).

  • zca (bool) – If True, use ZCA whitening, otherwise PCA whitening (default False).

  • bias (bool) – If True, divide by n when estimating the covariance; if False, use the unbiased estimator (divide by n - 1). Default is True.

mu

Mean of the training data along axis, computed by demean().

Type:

ndarray or None

sigma

Covariance matrix of the (demeaned) training data, computed by cov().

Type:

ndarray or None

W

The whitening matrix such that transform(X) = (X - mu) @ W.T.

Type:

ndarray or None

scale

Diagonal matrix diag(1/sqrt(eigval)) of inverse sqrt eigenvalues.

Type:

ndarray or None

U

Eigenvectors of the covariance matrix.

Type:

ndarray or None

Examples

>>> import numpy as np
>>> from pyeeg.preprocess import Whitener
>>> rng = np.random.default_rng(0)
>>> M = np.array([[2., 0.5, 0.1], [0.5, 1., 0.3], [0.1, 0.3, 1.5]])
>>> X = rng.standard_normal((100, 3)) @ M
>>> wh = Whitener(axis=0, zca=True).fit(X)
>>> Z = wh.transform(X)
>>> np.allclose(np.cov(Z, rowvar=False), np.eye(3), atol=1e-1)
True

Methods

Whitener.compute_rotation([C])

Compute the whitening rotation matrix from the (stored) covariance.

Whitener.cov(data[, axis])

Estimate the covariance matrix of data and store it in self.sigma.

Whitener.demean(data[, axis])

Subtract the mean of data along axis and store it in self.mu.

Whitener.fit(X[, y, axis])

Fit the whitener on X: demean, estimate the covariance and compute the whitening rotation.

Whitener.fit_transform(X[, y, axis])

Fit the whitener on X and return the whitened data.

Whitener.inverse(X[, axis])

Invert the whitening transform, approximately recovering the original (unwhitened) data.

Whitener.set_output(*[, transform])

Set output container.

Whitener.transform(X[, y])

Whiten X using the fitted mean and rotation.