def fit(self, inputs: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
        """Fit the K-means algorithm to the input data.

        Args:
            inputs (torch.Tensor): Input data to cluster.
                Shape: ``(n_samples, n_features)``.

        Returns:
            tuple[torch.Tensor, torch.Tensor]: Tuple containing:
                - labels: Cluster assignments for each input point.
                  Shape: ``(n_samples,)``
                - cluster_centers: Coordinates of the cluster centers.
                  Shape: ``(n_clusters, n_features)``

        Raises:
            ValueError: If ``n_clusters`` is less than or equal to 0.

        Example:
            >>> kmeans = KMeans(n_clusters=2)
            >>> data = torch.tensor([[1.0, 2.0], [4.0, 5.0], [1.2, 2.1]])
            >>> labels, centers = kmeans.fit(data)
            >>> print(f"Number of points in each cluster: {
            ...     [(labels == i).sum().item() for i in range(2)]
            ... }")
        """
        batch_size, _ = inputs.shape

        # Initialize centroids randomly from the data points
        centroid_indices = torch.randint(0, batch_size, (self.n_clusters,))
        self.cluster_centers_ = inputs[centroid_indices]

        # Run the k-means algorithm for max_iter iterations
        for _ in range(self.max_iter):
            # Compute the distance between each data point and each centroid
            distances = torch.cdist(inputs, self.cluster_centers_)

            # Assign each data point to the closest centroid
            self.labels_ = torch.argmin(distances, dim=1)

            # Update the centroids to be the mean of the data points assigned
            for j in range(self.n_clusters):
                mask = self.labels_ == j
                if mask.any():
                    self.cluster_centers_[j] = inputs[mask].mean(dim=0)

        return self.labels_, self.cluster_centers_
Fit the K-means algorithm to the input data.

        Args:
            inputs (torch.Tensor): Input data to cluster.
                Shape: ``(n_samples, n_features)``.

        Returns:
            tuple[torch.Tensor, torch.Tensor]: Tuple containing:
                - labels: Cluster assignments for each input point.
                  Shape: ``(n_samples,)``
                - cluster_centers: Coordinates of the cluster centers.
                  Shape: ``(n_clusters, n_features)``

        Raises:
            ValueError: If ``n_clusters`` is less than or equal to 0.

        Example:
            >>> kmeans = KMeans(n_clusters=2)
            >>> data = torch.tensor([[1.0, 2.0], [4.0, 5.0], [1.2, 2.1]])
            >>> labels, centers = kmeans.fit(data)
            >>> print(f"Number of points in each cluster: {
            ...     [(labels == i).sum().item() for i in range(2)]
            ... }")
        
src/anomalib/models/components/cluster/kmeans.py