PCA

class torchdr.PCA(n_components: int = 2, device: str = 'auto', verbose: bool = False, random_state: float = 0)[source]

Bases: DRModule

Principal Component Analysis module.

Parameters:
  • n_components (int, default=2) – Number of components to project the input data onto.

  • device (str, default="auto") – Device on which the computations are performed.

  • verbose (bool, default=False) – Whether to print information during the computations.

  • random_state (float, default=0) – Random seed for reproducibility.

fit(X: Tensor | ndarray)[source]

Fit the PCA model.

Parameters:

X (torch.Tensor or np.ndarray of shape (n_samples, n_features)) – Data on which to fit the PCA model.

Returns:

self – The fitted PCA model.

Return type:

PCA

fit_transform(X: Tensor | ndarray)[source]

Fit the PCA model and project the input data onto the components.

Parameters:

X (torch.Tensor or np.ndarray of shape (n_samples, n_features)) – Data on which to fit the PCA model and project onto the components.

Returns:

X_new – Projected data.

Return type:

torch.Tensor or np.ndarray of shape (n_samples, n_components)

transform(X: Tensor | ndarray)[source]

Project the input data onto the PCA components.

Parameters:

X (torch.Tensor or np.ndarray of shape (n_samples, n_features)) – Data to project onto the PCA components.

Returns:

X_new – Projected data.

Return type:

torch.Tensor or np.ndarray of shape (n_samples, n_components)

Examples using PCA:

PCA via SVD and via AffinityMatcher

PCA via SVD and via AffinityMatcher