def fit(self, dataset: torch.Tensor) -> None:
        """Fit the PCA model to the dataset.

        Args:
            dataset (torch.Tensor): Input dataset of shape ``(n_samples,
                n_features)``.

        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])
        """
        mean = dataset.mean(dim=0)
        dataset -= mean

        _, sig, v_h = torch.linalg.svd(dataset.double(), full_matrices=False)
        num_components: int
        if self.n_components <= 1:
            variance_ratios = torch.cumsum(sig * sig, dim=0) / torch.sum(sig * sig)
            num_components = torch.nonzero(variance_ratios >= self.n_components)[0]
        else:
            num_components = int(self.n_components)

        self.num_components = torch.tensor([num_components], device=dataset.device)

        self.singular_vectors = v_h.transpose(-2, -1)[:, :num_components].float()
        self.singular_values = sig[:num_components].float()
        self.mean = mean
Fit the PCA model to the dataset.

        Args:
            dataset (torch.Tensor): Input dataset of shape ``(n_samples,
                n_features)``.

        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])
        
src/anomalib/models/components/dimensionality_reduction/pca.py