Exemplo n.º 1
0
def spectral_clustering_sb(
    affinity,
    n_clusters=8,
    n_components=None,
    random_state=None,
    n_init=10,
):
    """Performs spectral clustering.

    Arguments
    ---------
    affinity : matrix
        Affinity matrix.
    n_clusters : int
        Number of clusters for kmeans.
    n_components : int
        Number of components to retain while estimating spectral embeddings.
    random_state : int
        A pseudo random number generator used by kmeans.
     n_init : int
        Number of time the k-means algorithm will be run with different centroid seeds.

    Returns
    -------
    labels : array
        Cluster label for each sample.

    Example
    -------
    >>> import numpy as np
    >>> from speechbrain.processing import diarization as diar
    >>> affinity = np.array([[1, 1, 1, 0.5, 0, 0, 0, 0, 0, 0.5],
    ... [1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
    ... [1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
    ... [0.5, 0, 0, 1, 1, 1, 0, 0, 0, 0],
    ... [0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
    ... [0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
    ... [0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
    ... [0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
    ... [0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
    ... [0.5, 0, 0, 0, 0, 0, 1, 1, 1, 1]])
    >>> labs = diar.spectral_clustering_sb(affinity, 3)
    >>> # print (labs) # [2 2 2 1 1 1 0 0 0 0]
    """

    random_state = _check_random_state(random_state)
    n_components = n_clusters if n_components is None else n_components

    maps = spectral_embedding_sb(
        affinity,
        n_components=n_components,
        drop_first=False,
    )

    _, labels, _ = k_means(maps,
                           n_clusters,
                           random_state=random_state,
                           n_init=n_init)

    return labels
Exemplo n.º 2
0
 def clusterSpectralEmbeddings(self, affinity, n_init=10, cuda=False):
     spectral_emb = self.getSpectralEmbeddings(affinity,
                                               n_spks=self.n_clusters,
                                               drop_first=False,
                                               cuda=cuda)
     _, labels, _ = k_means(spectral_emb,
                            self.n_clusters,
                            random_state=self.random_state,
                            n_init=n_init)
     return labels
Exemplo n.º 3
0
    def cluster_embs(self, emb, k):
        """Clusters the embeddings using kmeans.

        Arguments
        ---------
        emb : array (n_samples, n_components)
            Spectral embedding for each sample with n Eigen components.
        k : int
            Number of clusters to kmeans.

        Returns
        -------
        self.labels_ : self
            Labels for each sample embedding.
        """
        _, self.labels_, _ = k_means(emb, k)