(self, x, manual_init=None)
return cluster_centers
Initialize the k-means algorithm.

        Parameters
        ----------
        x : torch.Tensor
            Feature matrix of shape `(n_samples, n_features)`.

        manual_init : None or torch.Tensor
            If not None then expecting a tensor of shape `(n_clusters, n_features)`. Note that for this feature
            to be used one needs to set `init='manual'` in the constructor.

        Returns
        -------
        cluster_centers : torch.Tensor
            Tensor of shape `(n_clusters, n_features)` representing the initial cluster centers.
Initialize the k-means algorithm.
Initialize the k - means algorithm .
def initialize(self, x, manual_init=None):
        """Initialize the k-means algorithm.

        Parameters
        ----------
        x : torch.Tensor
            Feature matrix of shape `(n_samples, n_features)`.

        manual_init : None or torch.Tensor
            If not None then expecting a tensor of shape `(n_clusters, n_features)`. Note that for this feature
            to be used one needs to set `init='manual'` in the constructor.

        Returns
        -------
        cluster_centers : torch.Tensor
            Tensor of shape `(n_clusters, n_features)` representing the initial cluster centers.

        """
        n_samples, n_features = x.shape
        device, dtype = x.device, x.dtype

        # Note that normalization to probablities is done automatically within torch.multinomial
        if self.init == 'random':
            p = torch.ones(n_samples, dtype=dtype, device=device)
            # centroid_samples = torch.randperm(n_samples).to(device=device)[:self.n_clusters]
            centroid_samples = torch.multinomial(p, num_samples=self.n_clusters, replacement=False)
            cluster_centers = x[centroid_samples]

        elif self.init == 'k-means++':
            p = torch.ones(n_samples, dtype=dtype, device=device)
            cluster_centers_l = []
            centroid_samples_l = []

            while len(cluster_centers_l) < self.n_clusters:
                centroid_sample = torch.multinomial(p, num_samples=1, replacement=False)

                if centroid_sample in centroid_samples_l:
                    continue  # pragma: no cover
                centroid_samples_l.append(centroid_sample)

                cluster_center = x[[centroid_sample]]  # (1, n_features)
                cluster_centers_l.append(cluster_center)
                p = self.compute_distances(x, cluster_center).view(-1)

            cluster_centers = torch.cat(cluster_centers_l, dim=0)

        elif self.init == 'manual':
            if not torch.is_tensor(manual_init):
                raise TypeError('The manual_init needs to be a torch.Tensor')

            if manual_init.shape[0] != self.n_clusters:
                raise ValueError('The number of manually provided cluster centers is different from n_clusters')

            if manual_init.shape[1] != x.shape[1]:
                raise ValueError('The feature size of manually provided cluster centers is different from the input')

            cluster_centers = manual_init.to(dtype=dtype, device=device)

        return cluster_centers
def initialize ( self , x , manual_init = None ) : n_samples , n_features = x . shape device , dtype = x . device , x . dtype # Note that normalization to probablities is done automatically within torch.multinomial if self . init == 'random' : p = torch . ones ( n_samples , dtype = dtype , device = device ) # centroid_samples = torch.randperm(n_samples).to(device=device)[:self.n_clusters] centroid_samples = torch . multinomial ( p , num_samples = self . n_clusters , replacement = False ) cluster_centers = x [ centroid_samples ] elif self . init == 'k-means++' : p = torch . ones ( n_samples , dtype = dtype , device = device ) cluster_centers_l = [ ] centroid_samples_l = [ ] while len ( cluster_centers_l ) < self . n_clusters : centroid_sample = torch . multinomial ( p , num_samples = 1 , replacement = False ) if centroid_sample in centroid_samples_l : continue # pragma: no cover centroid_samples_l . append ( centroid_sample ) cluster_center = x [ [ centroid_sample ] ] # (1, n_features) cluster_centers_l . append ( cluster_center ) p = self . compute_distances ( x , cluster_center ) . view ( - 1 ) cluster_centers = torch . cat ( cluster_centers_l , dim = 0 ) elif self . init == 'manual' : if not torch . is_tensor ( manual_init ) : raise TypeError ( 'The manual_init needs to be a torch.Tensor' ) if manual_init . shape [ 0 ] != self . n_clusters : raise ValueError ( 'The number of manually provided cluster centers is different from n_clusters' ) if manual_init . shape [ 1 ] != x . shape [ 1 ] : raise ValueError ( 'The feature size of manually provided cluster centers is different from the input' ) cluster_centers = manual_init . to ( dtype = dtype , device = device ) return cluster_centers