Python networkx 模块,to_scipy_sparse_matrix() 实例源码

我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用networkx.to_scipy_sparse_matrix()

项目:GEM    作者:palash1992    | 项目源码 | 文件源码
def learn_embedding(self, graph=None, edge_f=None,
                        is_weighted=False, no_python=False):
        if not graph and not edge_f:
            raise Exception('graph/edge_f needed')
        if not graph:
            graph = graph_util.loadGraphFromEdgeListTxt(edge_f)
        graph = graph.to_undirected()
        t1 = time()
        A = nx.to_scipy_sparse_matrix(graph)
        normalize(A, norm='l1', axis=1, copy=False)
        I_n = sp.eye(graph.number_of_nodes())
        I_min_A = I_n - A
        u, s, vt = lg.svds(I_min_A, k=self._d + 1, which='SM')
        t2 = time()
        self._X = vt.T
        self._X = self._X[:, 1:]
        return self._X, (t2 - t1)
项目:dcnn    作者:jcatw    | 项目源码 | 文件源码
def parse_cora_sparse():
    path = "%s/../data/cora/" % (current_dir,)

    features, labels, id2index = _parse_cora_features_labels()

    n_papers = len(id2index)
    graph = nx.Graph()

    with open(path + 'cora.cites', 'r') as f:
        for line in f.xreadlines():
            items = line.strip().split('\t')

            tail = id2index[items[0]]
            head = id2index[items[1]]

            graph.add_edge(head, tail)

    adj = nx.to_scipy_sparse_matrix(graph, format='csr')

    return adj.astype('float32'), features.astype('float32'), labels.astype('int32')
项目:gym-kidney    作者:camoy    | 项目源码 | 文件源码
def _trans(g, deg):
    """
    Given graph g, and inverse degree matrix. Returns
    transition matrix for g (uniform over adjacent vertices).
    """
    adj = nx.to_scipy_sparse_matrix(g, format = "csc")
    return (deg * adj).T
项目:GEM    作者:palash1992    | 项目源码 | 文件源码
def learn_embedding(self, graph=None, edge_f=None,
                        is_weighted=False, no_python=False):
        if not graph and not edge_f:
            raise Exception('graph/edge_f needed')
        if not graph:
            graph = graph_util.loadGraphFromEdgeListTxt(edge_f)

        t1 = time()
        # A = nx.to_scipy_sparse_matrix(graph)
        # I = sp.eye(graph.number_of_nodes())
        # M_g = I - self._beta*A
        # M_l = self._beta*A
        A = nx.to_numpy_matrix(graph)
        M_g = np.eye(graph.number_of_nodes()) - self._beta * A
        M_l = self._beta * A
        S = np.dot(np.linalg.inv(M_g), M_l)

        u, s, vt = lg.svds(S, k=self._d // 2)
        X1 = np.dot(u, np.diag(np.sqrt(s)))
        X2 = np.dot(vt.T, np.diag(np.sqrt(s)))
        t2 = time()
        self._X = np.concatenate((X1, X2), axis=1)

        p_d_p_t = np.dot(u, np.dot(np.diag(s), vt))
        eig_err = np.linalg.norm(p_d_p_t - S)
        print('SVD error (low rank): %f' % eig_err)
        return self._X, (t2 - t1)
项目:PedWorks    作者:BrnCPrz    | 项目源码 | 文件源码
def directed_modularity_matrix(G, nodelist=None):
    """ INCLUDED FOR TESTING PURPOSES - Not implemented yet.

    Return the directed modularity matrix of G.
    The modularity matrix is the matrix B = A - <A>, where A is the adjacency
    matrix and <A> is the expected adjacency matrix, assuming that the graph
    is described by the configuration model.
    More specifically, the element B_ij of B is defined as
        B_ij = A_ij - k_i(out) k_j(in)/m
    where k_i(in) is the in degree of node i, and k_j(out) is the out degree
    of node j, with m the number of edges in the graph.
    Parameters
    ----------
    G : DiGraph
       A NetworkX DiGraph
    nodelist : list, optional
       The rows and columns are ordered according to the nodes in nodelist.
       If nodelist is None, then the ordering is produced by G.nodes().
    Returns
    -------
    B : Numpy matrix
      The modularity matrix of G.
    Notes
    -----
    NetworkX defines the element A_ij of the adjacency matrix as 1 if there
    is a link going from node i to node j. Leicht and Newman use the opposite
    definition. This explains the different expression for B_ij.
    See Also
    --------
    to_numpy_matrix
    adjacency_matrix
    laplacian_matrix
    modularity_matrix
    References
    ----------
    .. [1] E. A. Leicht, M. E. J. Newman,
       "Community structure in directed networks",
        Phys. Rev Lett., vol. 100, no. 11, p. 118703, 2008.
    """
    if nodelist is None:
        nodelist = G.nodes()
    A = nx.to_scipy_sparse_matrix(G, nodelist=nodelist, format='csr')
    k_in = A.sum(axis=0)
    k_out = A.sum(axis=1)
    m = G.number_of_edges()
    # Expected adjacency matrix
    X = k_out * k_in / m
    return A - X
项目:scanpy    作者:theislab    | 项目源码 | 文件源码
def aga_contract_graph(adata, min_group_size=0.01, max_n_contractions=1000, copy=False):
    """Contract the abstracted graph.
    """
    adata = adata.copy() if copy else adata
    if 'aga_adjacency_tree_confidence' not in adata.uns: raise ValueError('run tool aga first!')
    min_group_size = min_group_size if min_group_size >= 1 else int(min_group_size * adata.n_smps)
    logg.info('contract graph using `min_group_size={}`'.format(min_group_size))

    def propose_nodes_to_contract(adjacency_tree_confidence, node_groups):
        # nodes with two edges
        n_edges_per_seg = np.sum(adjacency_tree_confidence > 0, axis=1).A1
        for i in range(adjacency_tree_confidence.shape[0]):
            if n_edges_per_seg[i] == 2:
                neighbors = adjacency_tree_confidence[i].nonzero()[1]
                for neighbors_edges in range(1, 20):
                    for n_cnt, n in enumerate(neighbors):
                        if n_edges_per_seg[n] == neighbors_edges:
                            logg.msg('merging node {} into {} (two edges)'
                                   .format(i, n), v=4)
                            return i, n
        # node groups with a very small cell number
        for i in range(adjacency_tree_confidence.shape[0]):
            if node_groups[str(i) == node_groups].size < min_group_size:
                neighbors = adjacency_tree_confidence[i].nonzero()[1]
                neighbor_sizes = [node_groups[str(n) == node_groups].size for n in neighbors]
                n = neighbors[np.argmax(neighbor_sizes)]
                logg.msg('merging node {} into {} '
                       '(smaller than `min_group_size` = {})'
                       .format(i, n, min_group_size), v=4)
                return i, n
        return 0, 0

    def contract_nodes(adjacency_tree_confidence, node_groups):
        for count in range(max_n_contractions):
            i, n = propose_nodes_to_contract(adjacency_tree_confidence, node_groups)
            if i != 0 or n != 0:
                G = nx.Graph(adjacency_tree_confidence)
                G_contracted = nx.contracted_nodes(G, n, i, self_loops=False)
                adjacency_tree_confidence = nx.to_scipy_sparse_matrix(G_contracted)
                node_groups[str(i) == node_groups] = str(n)
                for j in range(i+1, G.size()+1):
                    node_groups[str(j) == node_groups] = str(j-1)
            else:
                break
        return adjacency_tree_confidence, node_groups

    size_before = adata.uns['aga_adjacency_tree_confidence'].shape[0]
    adata.uns['aga_adjacency_tree_confidence'], adata.smp['aga_groups'] = contract_nodes(
        adata.uns['aga_adjacency_tree_confidence'], adata.smp['aga_groups'].values)
    adata.uns['aga_groups_order'] = np.unique(adata.smp['aga_groups'].values)
    for key in ['aga_adjacency_full_confidence', 'aga_groups_original',
                'aga_groups_order_original', 'aga_groups_colors_original']:
        if key in adata.uns: del adata.uns[key]
    logg.info('    contracted graph from {} to {} nodes'
              .format(size_before, adata.uns['aga_adjacency_tree_confidence'].shape[0]))
    logg.msg('removed adata.uns["aga_adjacency_full_confidence"]', v=4)
    return adata if copy else None