コード例 #1
0
    def fit(
        self, input_matrix: Union[sparse.csr_matrix, np.ndarray]
    ) -> 'PropagationClustering':
        """Clustering by label propagation.

        Parameters
        ----------
        input_matrix :
            Adjacency matrix or biadjacency matrix of the graph.

        Returns
        -------
        self: :class:`PropagationClustering`
        """
        self._init_vars()

        # input
        adjacency, bipartite = get_adjacency(input_matrix)

        # propagation
        Propagation.fit(self, adjacency)

        # output
        _, self.labels_ = np.unique(self.labels_, return_inverse=True)
        if bipartite:
            self._split_vars(input_matrix.shape)
            self.bipartite = True
        self._secondary_outputs(input_matrix)

        return self
コード例 #2
0
    def fit(self, input_matrix: Union[sparse.csr_matrix, np.ndarray]):
        """Embedding of graphs from a clustering obtained with Louvain.

        Parameters
        ----------
        input_matrix :
            Adjacency matrix or biadjacency matrix of the graph.

        Returns
        -------
        self: :class:`LouvainNE`
        """
        # input
        adjacency, self.bipartite = get_adjacency(input_matrix)
        n = adjacency.shape[0]

        # embedding
        self.embedding_ = np.zeros((n, self.n_components))
        self._recursive_louvain(adjacency, 0)

        if self.bipartite:
            self._split_vars(input_matrix.shape)
        return self
コード例 #3
0
    def fit(self, input_matrix: Union[sparse.csr_matrix, np.ndarray], force_bipartite: bool = False):
        """Embedding of graphs from a clustering obtained with Louvain.

        Parameters
        ----------
        input_matrix :
            Adjacency matrix or biadjacency matrix of the graph.
        force_bipartite :
            If ``True``, force the input matrix to be considered as a biadjacency matrix even if square.
        Returns
        -------
        self: :class:`LouvainNE`
        """
        # input
        adjacency, self.bipartite = get_adjacency(input_matrix, force_bipartite=force_bipartite)
        n = adjacency.shape[0]

        # embedding
        self.embedding_ = np.zeros((n, self.n_components))
        self._recursive_louvain(adjacency, 0)

        if self.bipartite:
            self._split_vars(input_matrix.shape)
        return self
コード例 #4
0
ファイル: katz.py プロジェクト: vishalbelsare/scikit-network
    def fit(
        self, input_matrix: Union[sparse.csr_matrix, np.ndarray,
                                  LinearOperator]
    ) -> 'Katz':
        """Katz centrality.

        Parameters
        ----------
        input_matrix :
            Adjacency matrix or biadjacency matrix of the graph.

        Returns
        -------
        self: :class:`Katz`
        """
        adjacency, self.bipartite = get_adjacency(input_matrix)
        n = adjacency.shape[0]
        coefs = self.damping_factor**np.arange(self.path_length + 1)
        coefs[0] = 0.
        polynome = Polynome(adjacency.T.astype(bool).tocsr(), coefs)
        self.scores_ = polynome.dot(np.ones(n))
        if self.bipartite:
            self._split_vars(input_matrix.shape)
        return self
コード例 #5
0
    def fit(
        self, input_matrix: Union[sparse.csr_matrix,
                                  np.ndarray]) -> 'LouvainHierarchy':
        """Fit algorithm to data.

        Parameters
        ----------
        input_matrix :
            Adjacency matrix or biadjacency matrix of the graph.

        Returns
        -------
        self: :class:`LouvainHierarchy`
        """
        self._init_vars()
        adjacency, self.bipartite = get_adjacency(input_matrix)
        tree = self._recursive_louvain(adjacency, self.depth)
        dendrogram, _ = get_dendrogram(tree)
        dendrogram = np.array(dendrogram)
        dendrogram[:, 2] -= min(dendrogram[:, 2])
        self.dendrogram_ = reorder_dendrogram(dendrogram)
        if self.bipartite:
            self._split_vars(input_matrix.shape)
        return self
コード例 #6
0
    def fit(self, input_matrix: Union[sparse.csr_matrix, np.ndarray], force_bipartite: bool = False) -> 'Louvain':
        """Fit algorithm to data.

        Parameters
        ----------
        input_matrix :
            Adjacency matrix or biadjacency matrix of the graph.
        force_bipartite :
            If ``True``, force the input matrix to be considered as a biadjacency matrix even if square.

        Returns
        -------
        self: :class:`Louvain`
        """
        self._init_vars()

        if self.modularity == 'dugue':
            adjacency, self.bipartite = get_adjacency(input_matrix, force_directed=True,
                                                      force_bipartite=force_bipartite)
        else:
            adjacency, self.bipartite = get_adjacency(input_matrix, force_bipartite=force_bipartite)

        n = adjacency.shape[0]

        if self.modularity == 'potts':
            probs_out = get_probs('uniform', adjacency)
            probs_in = probs_out.copy()
        elif self.modularity == 'newman':
            probs_out = get_probs('degree', adjacency)
            probs_in = probs_out.copy()
        elif self.modularity == 'dugue':
            probs_out = get_probs('degree', adjacency)
            probs_in = get_probs('degree', adjacency.T)
        else:
            raise ValueError('Unknown modularity function.')

        nodes = np.arange(n)
        if self.shuffle_nodes:
            nodes = self.random_state.permutation(nodes)
            adjacency = adjacency[nodes, :].tocsc()[:, nodes].tocsr()

        adjacency_cluster = adjacency / adjacency.data.sum()

        membership = sparse.identity(n, format='csr')
        increase = True
        count_aggregations = 0
        self.log.print("Starting with", n, "nodes.")
        while increase:
            count_aggregations += 1

            labels_cluster, pass_increase = self._optimize(adjacency_cluster, probs_out, probs_in)
            _, labels_cluster = np.unique(labels_cluster, return_inverse=True)

            if pass_increase <= self.tol_aggregation:
                increase = False
            else:
                membership_cluster = membership_matrix(labels_cluster)
                membership = membership.dot(membership_cluster)
                adjacency_cluster, probs_out, probs_in = self._aggregate(adjacency_cluster, probs_out, probs_in,
                                                                        membership_cluster)

                n = adjacency_cluster.shape[0]
                if n == 1:
                    break
            self.log.print("Aggregation", count_aggregations, "completed with", n, "clusters and ",
                           pass_increase, "increment.")
            if count_aggregations == self.n_aggregations:
                break

        if self.sort_clusters:
            labels = reindex_labels(membership.indices)
        else:
            labels = membership.indices
        if self.shuffle_nodes:
            reverse = np.empty(nodes.size, nodes.dtype)
            reverse[nodes] = np.arange(nodes.size)
            labels = labels[reverse]

        self.labels_ = labels
        if self.bipartite:
            self._split_vars(input_matrix.shape)
        self._secondary_outputs(input_matrix)

        return self
コード例 #7
0
    def fit(self,
            input_matrix: Union[sparse.csr_matrix, np.ndarray],
            force_bipartite: bool = False) -> 'Spectral':
        """Compute the graph embedding.

        If the input matrix :math:`B` is not square (e.g., biadjacency matrix of a bipartite graph) or not symmetric
        (e.g., adjacency matrix of a directed graph), use the adjacency matrix

        :math:`A  = \\begin{bmatrix} 0 & B \\\\ B^T & 0 \\end{bmatrix}`

        and return the embedding for both rows and columns of the input matrix :math:`B`.

        Parameters
        ----------
        input_matrix :
              Adjacency matrix or biadjacency matrix of the graph.
        force_bipartite : bool (default = ``False``)
            If ``True``, force the input matrix to be considered as a biadjacency matrix.

        Returns
        -------
        self: :class:`Spectral`
        """
        # input
        adjacency, self.bipartite = get_adjacency(
            input_matrix,
            allow_directed=False,
            force_bipartite=force_bipartite)
        n = adjacency.shape[0]

        # regularization
        regularization = self._get_regularization(self.regularization,
                                                  adjacency)
        self.regularized = regularization > 0

        # laplacian
        laplacian = Laplacian(adjacency, regularization,
                              self.normalized_laplacian)

        # spectral decomposition
        n_components = check_n_components(self.n_components, n - 2) + 1
        solver = LanczosEig(which='SM')
        solver.fit(matrix=laplacian, n_components=n_components)
        index = np.argsort(
            solver.eigenvalues_)[1:]  # increasing order, skip first

        eigenvalues = solver.eigenvalues_[index]
        eigenvectors = solver.eigenvectors_[:, index]

        # embedding
        if self.normalized_laplacian:
            embedding = laplacian.norm_diag.dot(eigenvectors)
        else:
            embedding = eigenvectors.copy()

        # output
        self.embedding_ = embedding
        self.eigenvalues_ = eigenvalues
        self.eigenvectors_ = eigenvectors
        if self.bipartite:
            self._split_vars(input_matrix.shape)

        return self