Dimensionality Reduction#
Dimensionality reduction and decomposition algorithms for feature processing.
This module provides implementations of dimensionality reduction techniques used in anomaly detection models.
- Classes:
PCA: Principal Component Analysis for linear dimensionality reduction. SparseRandomProjection: Random projection using sparse random matrices.
Example
>>> from anomalib.models.components.dimensionality_reduction import PCA
>>> # Create and fit PCA
>>> pca = PCA(n_components=10)
>>> features = torch.randn(100, 50) # 100 samples, 50 features
>>> reduced_features = pca.fit_transform(features)
>>> # Use SparseRandomProjection
>>> from anomalib.models.components.dimensionality_reduction import (
... SparseRandomProjection
... )
>>> projector = SparseRandomProjection(n_components=20)
>>> projected_features = projector.fit_transform(features)
- class anomalib.models.components.dimensionality_reduction.PCA(n_components)#
Bases:
DynamicBufferMixin
Principal Component Analysis (PCA) for dimensionality reduction.
- Parameters:
n_components (int | float) – Number of components to keep. If float between 0 and 1, represents the variance ratio to preserve. If int, represents the exact number of components to keep.
- singular_vectors#
Right singular vectors from SVD.
- Type:
- singular_values#
Singular values from SVD.
- Type:
- mean#
Mean of the training data.
- Type:
- num_components#
Number of components kept.
- Type:
Example
>>> import torch >>> from anomalib.models.components import PCA >>> # Create sample data >>> data = torch.randn(100, 10) # 100 samples, 10 features >>> # Initialize with fixed number of components >>> pca = PCA(n_components=3) >>> pca.fit(data) >>> # Transform new data >>> transformed = pca.transform(data) >>> print(transformed.shape) torch.Size([100, 3]) >>> # Initialize with variance ratio >>> pca = PCA(n_components=0.95) # Keep 95% of variance >>> pca.fit(data)
- fit(dataset)#
Fit the PCA model to the dataset.
- Parameters:
dataset (torch.Tensor) – Input dataset of shape
(n_samples, n_features)
.- Return type:
Example
>>> data = torch.randn(100, 10) >>> pca = PCA(n_components=3) >>> pca.fit(data) >>> # Access fitted attributes >>> print(pca.singular_vectors.shape) torch.Size([10, 3]) >>> print(pca.mean.shape) torch.Size([10])
- fit_transform(dataset)#
Fit the model and transform the input dataset.
- Parameters:
dataset (torch.Tensor) – Input dataset of shape
(n_samples, n_features)
.- Returns:
- Transformed dataset of shape ``(n_samples,
n_components)``.
- Return type:
Example
>>> data = torch.randn(100, 10) >>> pca = PCA(n_components=3) >>> transformed = pca.fit_transform(data) >>> print(transformed.shape) torch.Size([100, 3])
- forward(features)#
Transform features (alias for transform method).
- Parameters:
features (torch.Tensor) – Input features of shape
(n_samples, n_features)
.- Returns:
- Transformed features of shape ``(n_samples,
n_components)``.
- Return type:
Example
>>> data = torch.randn(100, 10) >>> pca = PCA(n_components=3) >>> pca.fit(data) >>> transformed = pca(data) # Using forward >>> print(transformed.shape) torch.Size([100, 3])
- inverse_transform(features)#
Inverse transform features back to original space.
- Parameters:
features (torch.Tensor) – Transformed features of shape
(n_samples, n_components)
.- Returns:
- Reconstructed features of shape ``(n_samples,
n_features)``.
- Return type:
Example
>>> data = torch.randn(100, 10) >>> pca = PCA(n_components=3) >>> transformed = pca.fit_transform(data) >>> reconstructed = pca.inverse_transform(transformed) >>> print(reconstructed.shape) torch.Size([100, 10])
- transform(features)#
Transform features using the fitted PCA model.
- Parameters:
features (torch.Tensor) – Input features of shape
(n_samples, n_features)
.- Returns:
- Transformed features of shape ``(n_samples,
n_components)``.
- Return type:
Example
>>> data = torch.randn(100, 10) >>> pca = PCA(n_components=3) >>> pca.fit(data) >>> new_data = torch.randn(50, 10) >>> transformed = pca.transform(new_data) >>> print(transformed.shape) torch.Size([50, 3])
- class anomalib.models.components.dimensionality_reduction.SparseRandomProjection(eps=0.1, random_state=None)#
Bases:
object
Sparse Random Projection using PyTorch operations.
This class implements sparse random projection for dimensionality reduction using PyTorch. The implementation is based on the paper by Li et al. [1]_.
- Parameters:
- sparse_random_matrix#
Random projection matrix.
- Type:
Example
>>> import torch >>> from anomalib.models.components import SparseRandomProjection >>> # Create sample data >>> data = torch.randn(100, 50) # 100 samples, 50 features >>> # Initialize and fit projector >>> projector = SparseRandomProjection(eps=0.1) >>> projector.fit(data) >>> # Transform data >>> projected = projector.transform(data) >>> print(projected.shape)
References
- fit(embedding)#
Fit the random projection matrix to the data.
- Parameters:
embedding (torch.Tensor) – Input tensor of shape
(n_samples, n_features)
.- Returns:
The fitted projector.
- Return type:
Example
>>> projector = SparseRandomProjection() >>> data = torch.randn(100, 50) >>> projector = projector.fit(data)
- transform(embedding)#
Project the data using the random projection matrix.
- Parameters:
embedding (torch.Tensor) – Input tensor of shape
(n_samples, n_features)
.- Returns:
- Projected tensor of shape
(n_samples, n_components)
.
- Return type:
- Raises:
NotFittedError – If transform is called before fitting.
Example
>>> projector = SparseRandomProjection() >>> data = torch.randn(100, 50) >>> projector.fit(data) >>> projected = projector.transform(data) >>> print(projected.shape)