How do I transform a “SciPy sparse matrix” to a “NumPy matrix”?

前端 未结 3 1034
情书的邮戳
情书的邮戳 2021-01-03 22:34

I am using a python function called \"incidence_matrix(G)\", which returns the incident matrix of graph. It is from Networkx package. The problem that I am facing is the ret

3条回答
  •  庸人自扰
    2021-01-03 22:56

    The simplest way is to call the todense() method on the data:

    In [1]: import networkx as nx
    
    In [2]: G = nx.Graph([(1,2)])
    
    In [3]: nx.incidence_matrix(G)
    Out[3]: 
    <2x1 sparse matrix of type ''
        with 2 stored elements in Compressed Sparse Column format>
    
    In [4]: nx.incidence_matrix(G).todense()
    Out[4]: 
    matrix([[ 1.],
            [ 1.]])
    
    In [5]: nx.incidence_matrix(G).todense().A
    Out[5]: 
    array([[ 1.],
           [ 1.]])
    

提交回复
热议问题