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 meanmuis stored duringfit()(viademean()) so thattransform()andinverse()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
nwhen estimating the covariance; if False, use the unbiased estimator (divide byn - 1). Default is True.
- 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
Compute the whitening rotation matrix from the (stored) covariance.
Whitener.cov(data[, axis])Estimate the covariance matrix of
dataand store it inself.sigma.
Whitener.demean(data[, axis])Subtract the mean of
dataalongaxisand store it inself.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
Xand 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
Xusing the fitted mean and rotation.