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:

torch.Tensor

singular_values#

Singular values from SVD.

Type:

torch.Tensor

mean#

Mean of the training data.

Type:

torch.Tensor

num_components#

Number of components kept.

Type:

torch.Tensor

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:

None

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:

torch.Tensor

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:

torch.Tensor

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:

torch.Tensor

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:

torch.Tensor

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:
  • eps (float, optional) – Minimum distortion rate parameter for calculating Johnson-Lindenstrauss minimum dimensions. Defaults to 0.1.

  • random_state (int | None, optional) – Seed for random number generation. Used for reproducible results. Defaults to None.

n_components#

Number of components in the projected space.

Type:

int

sparse_random_matrix#

Random projection matrix.

Type:

torch.Tensor

eps#

Minimum distortion rate.

Type:

float

random_state#

Random seed.

Type:

int | None

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:

SparseRandomProjection

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:

torch.Tensor

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)