pca#

diffsptk.PCA#

alias of PrincipalComponentAnalysis

class diffsptk.PrincipalComponentAnalysis(order: int, n_comp: int, *, cov_type: str | int = 'sample', sort: str = 'descending', batch_size: int | None = None, verbose: bool = False, device: device | None = None, dtype: dtype | None = None)[source]#

See this page for details. Note that the forward method is not differentiable.

Parameters:
orderint >= 0

The order of the vector, \(M\).

n_compint >= 1

The number of principal components, \(K\).

cov_type[‘sample’, ‘unbiased’, ‘correlation’]

The type of covariance matrix.

sort[‘ascending’, ‘descending’]

The order of eigenvalues.

batch_sizeint >= 1 or None

The batch size.

verbosebool

If True, shows progress bars.

devicetorch.device or None

The device of this module.

dtypetorch.dtype or None

The data type of this module.

center(x: Tensor) Tensor[source]#

Center the input vectors using the estimated mean.

Parameters:
xTensor [shape=(…, M+1)]

The input vectors.

Returns:
outTensor [shape=(…, M+1)]

The centered vectors.

forward(x: Tensor | DataLoader) tuple[Tensor, Tensor, Tensor][source]#

Perform principal component analysis.

Parameters:
xTensor [shape=(T, M+1)] or DataLoader

The input vectors or a DataLoader that yields the input vectors.

Returns:
sTensor [shape=(K,)]

The eigenvalues.

VTensor [shape=(K, M+1)]

The eigenvectors.

mTensor [shape=(M+1,)]

The mean vector.

Examples

>>> import diffsptk
>>> import torch
>>> pca = diffsptk.PCA(1, 1)
>>> x = torch.tensor([
...     [-0.5, 0.3], [0.0, 0.7], [0.2, -0.1], [3.4, 2.0], [-2.8, 1.0],
...     [2.9, -3.0], [2.2, -2.5], [1.5, -1.6], [1.8, 0.5], [1.3, 0.0],
... ])
>>> s, _, _ = pca(x)
>>> s
tensor([3.6372])
>>> y = pca.transform(x)
>>> y.shape
torch.Size([10, 1])
transform(x: Tensor) Tensor[source]#

Transform the input vectors using the estimated eigenvectors.

Parameters:
xTensor [shape=(…, M+1)]

The input vectors.

Returns:
outTensor [shape=(…, K)]

The transformed vectors.

whiten(x: Tensor) Tensor[source]#

Whiten the input vectors using the estimated parameters.

Parameters:
xTensor [shape=(…, M+1)]

The input vectors.

Returns:
outTensor [shape=(…, K)]

The whitened vectors.

See also

nmf ica lbg