mlcolvar.core.stats.PCA

class mlcolvar.core.stats.PCA(in_features: int, out_features: int = None)[source]

Bases: Stats

Principal Component Analysis class.

Variables:
  • in_features (int) – Number of features

  • out_features (int) – Number of output features

  • evals (torch.Tensor) – PCA eigenvalues

  • evecs (torch.Tensor) – PCA eigenvectors

__init__(in_features: int, out_features: int = None)[source]

Initialize a PCA object. If out_features (<in_features) is given a low rank approximation will be performed.

Parameters:
  • in_features (int) – number of input features

  • out_features (int, optional) – number of principal components, by default None (equal to in_features)

Methods

__init__(in_features[, out_features])

Initialize a PCA object.

compute(X[, save_params])

Compute PCA eigenvalues and eigenvectors via torch.pca_lowrank method.

extra_repr()

Return the extra representation of the module.

forward(x)

Compute linear combination with saved eigenvectors.

compute(X, save_params=True, **kwargs)[source]

Compute PCA eigenvalues and eigenvectors via torch.pca_lowrank method.

Parameters:
  • X (array-like of shape (n_samples, in_features)) – Training data.

  • save_params (bool, optional) – Whether to store parameters in model

  • kwargs (optional) – Keyword arguments passed to torch.pca_lowrank (e.g. center)

Returns:

  • eigvals (torch.Tensor) – PCA eigenvalues

  • eigvecs (torch.Tensor) – PCA eigenvectors

Notes

The eigenvecs object which is returned is a matrix whose column eigvecs[:,i] is the eigenvector associated to eigvals[i]

property cumulative_explained_variance

Cumulative explained variance.

Returns:

cumulative variance ratio

Return type:

cum_exp_var, torch.Tensor

property explained_variance

Explained variance ratio of the principal components selected.

Returns:

explained variance ratio

Return type:

exp_var, torch.Tensor

extra_repr() str[source]

Return the extra representation of the module.

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

forward(x: Tensor) Tensor[source]

Compute linear combination with saved eigenvectors. If self.out_features is < then the number of eigenvectors only the first out_features components will be used.

Parameters:

x (torch.Tensor) – input

Returns:

out – output

Return type:

torch.Tensor

Attributes

T_destination

call_super_init

cumulative_explained_variance

Cumulative explained variance.

dump_patches

explained_variance

Explained variance ratio of the principal components selected.

training