Python NetworkX:当我添加';重量';对于某些节点,我无法生成邻接矩阵()?

Python NetworkX:当我添加';重量';对于某些节点,我无法生成邻接矩阵()?,python,matrix,graph,networkx,Python,Matrix,Graph,Networkx,当我向节点添加“权重”时,我就不能再生成邻接矩阵()?有没有关于如何仍然能够生成它的想法 In [73]: g2 = nx.Graph() In [74]: g2.add_path([1,2,3,5,4,3,1,4,3,7,2]) In [75]: nx.adjacency_matrix(g2) Out[75]: matrix([[ 0., 1., 1., 1., 0., 0.], [ 1., 0., 1., 0., 0., 1.], [ 1., 1.

当我向节点添加“权重”时,我就不能再生成邻接矩阵()?有没有关于如何仍然能够生成它的想法

In [73]: g2 = nx.Graph()

In [74]: g2.add_path([1,2,3,5,4,3,1,4,3,7,2])

In [75]: nx.adjacency_matrix(g2)
Out[75]: 
matrix([[ 0.,  1.,  1.,  1.,  0.,  0.],
    [ 1.,  0.,  1.,  0.,  0.,  1.],
    [ 1.,  1.,  0.,  1.,  1.,  1.],
    [ 1.,  0.,  1.,  0.,  1.,  0.],
    [ 0.,  0.,  1.,  1.,  0.,  0.],
    [ 0.,  1.,  1.,  0.,  0.,  0.]])

In [76]: g2[3]['weight'] = 5

In [77]: nx.adjacency_matrix(g2)
---------------------------------------------------------------------------
 AttributeError                            Traceback (most recent call last)
 <ipython-input-77-532c786b4588> in <module>()
 ----> 1 nx.adjacency_matrix(g2)

/usr/lib/pymodules/python2.7/networkx/linalg/graphmatrix.pyc in  adjacency_matrix(G, nodelist, weight)
     144     to_dict_of_dicts
     145     """
--> 146     return nx.to_numpy_matrix(G,nodelist=nodelist,weight=weight)
    147 
    148 adj_matrix=adjacency_matrix

/usr/lib/pymodules/python2.7/networkx/convert.pyc in to_numpy_matrix(G, nodelist, dtype, order, multigraph_weight, weight)
    522             for v,d in nbrdict.items():
    523                 try:
--> 524                     M[index[u],index[v]]=d.get(weight,1)
    525                 except KeyError:
    526                     pass

AttributeError: 'int' object has no attribute 'get'

您已接近-将节点权重分配给g2。节点[2]['weight']将正常工作

但请注意,节点权重不会出现在邻接矩阵中。在此处指定的是边权重。比如说

In [1]: import networkx as nx

In [2]: g2 = nx.Graph()

In [3]: g2.add_path([1,2,3,5,4,3,1,4,3,7,2])

In [4]: g2.node[2]['weight']=7

In [5]: g2.node
Out[5]: {1: {}, 2: {'weight': 7}, 3: {}, 4: {}, 5: {}, 7: {}}

In [6]: nx.adjacency_matrix(g2).todense()
Out[6]: 
matrix([[0, 1, 1, 1, 0, 0],
        [1, 0, 1, 0, 0, 1],
        [1, 1, 0, 1, 1, 1],
        [1, 0, 1, 0, 1, 0],
        [0, 0, 1, 1, 0, 0],
        [0, 1, 1, 0, 0, 0]])

In [7]: g2.edge[1][2]['weight'] = 42

In [8]: nx.adjacency_matrix(g2).todense()
Out[8]: 
matrix([[ 0, 42,  1,  1,  0,  0],
        [42,  0,  1,  0,  0,  1],
        [ 1,  1,  0,  1,  1,  1],
        [ 1,  0,  1,  0,  1,  0],
        [ 0,  0,  1,  1,  0,  0],
        [ 0,  1,  1,  0,  0,  0]])

您还将看到,我正在使用生成稀疏矩阵的networkx的更新版本,因此我添加了.todense()方法以获得密集(numpy)矩阵。

您很接近-将节点权重分配给
g2.node[2]['weight']
,它将起作用

但请注意,节点权重不会出现在邻接矩阵中。在此处指定的是边权重。比如说

In [1]: import networkx as nx

In [2]: g2 = nx.Graph()

In [3]: g2.add_path([1,2,3,5,4,3,1,4,3,7,2])

In [4]: g2.node[2]['weight']=7

In [5]: g2.node
Out[5]: {1: {}, 2: {'weight': 7}, 3: {}, 4: {}, 5: {}, 7: {}}

In [6]: nx.adjacency_matrix(g2).todense()
Out[6]: 
matrix([[0, 1, 1, 1, 0, 0],
        [1, 0, 1, 0, 0, 1],
        [1, 1, 0, 1, 1, 1],
        [1, 0, 1, 0, 1, 0],
        [0, 0, 1, 1, 0, 0],
        [0, 1, 1, 0, 0, 0]])

In [7]: g2.edge[1][2]['weight'] = 42

In [8]: nx.adjacency_matrix(g2).todense()
Out[8]: 
matrix([[ 0, 42,  1,  1,  0,  0],
        [42,  0,  1,  0,  0,  1],
        [ 1,  1,  0,  1,  1,  1],
        [ 1,  0,  1,  0,  1,  0],
        [ 0,  0,  1,  1,  0,  0],
        [ 0,  1,  1,  0,  0,  0]])

您还将看到,我正在使用生成稀疏矩阵的networkx的更新版本,因此我添加了.todense()方法以获得密集(numpy)矩阵。

您很接近-将节点权重分配给
g2.node[2]['weight']
,它将起作用

但请注意,节点权重不会出现在邻接矩阵中。在此处指定的是边权重。比如说

In [1]: import networkx as nx

In [2]: g2 = nx.Graph()

In [3]: g2.add_path([1,2,3,5,4,3,1,4,3,7,2])

In [4]: g2.node[2]['weight']=7

In [5]: g2.node
Out[5]: {1: {}, 2: {'weight': 7}, 3: {}, 4: {}, 5: {}, 7: {}}

In [6]: nx.adjacency_matrix(g2).todense()
Out[6]: 
matrix([[0, 1, 1, 1, 0, 0],
        [1, 0, 1, 0, 0, 1],
        [1, 1, 0, 1, 1, 1],
        [1, 0, 1, 0, 1, 0],
        [0, 0, 1, 1, 0, 0],
        [0, 1, 1, 0, 0, 0]])

In [7]: g2.edge[1][2]['weight'] = 42

In [8]: nx.adjacency_matrix(g2).todense()
Out[8]: 
matrix([[ 0, 42,  1,  1,  0,  0],
        [42,  0,  1,  0,  0,  1],
        [ 1,  1,  0,  1,  1,  1],
        [ 1,  0,  1,  0,  1,  0],
        [ 0,  0,  1,  1,  0,  0],
        [ 0,  1,  1,  0,  0,  0]])

您还将看到,我正在使用生成稀疏矩阵的networkx的更新版本,因此我添加了.todense()方法以获得密集(numpy)矩阵。

您很接近-将节点权重分配给
g2.node[2]['weight']
,它将起作用

但请注意,节点权重不会出现在邻接矩阵中。在此处指定的是边权重。比如说

In [1]: import networkx as nx

In [2]: g2 = nx.Graph()

In [3]: g2.add_path([1,2,3,5,4,3,1,4,3,7,2])

In [4]: g2.node[2]['weight']=7

In [5]: g2.node
Out[5]: {1: {}, 2: {'weight': 7}, 3: {}, 4: {}, 5: {}, 7: {}}

In [6]: nx.adjacency_matrix(g2).todense()
Out[6]: 
matrix([[0, 1, 1, 1, 0, 0],
        [1, 0, 1, 0, 0, 1],
        [1, 1, 0, 1, 1, 1],
        [1, 0, 1, 0, 1, 0],
        [0, 0, 1, 1, 0, 0],
        [0, 1, 1, 0, 0, 0]])

In [7]: g2.edge[1][2]['weight'] = 42

In [8]: nx.adjacency_matrix(g2).todense()
Out[8]: 
matrix([[ 0, 42,  1,  1,  0,  0],
        [42,  0,  1,  0,  0,  1],
        [ 1,  1,  0,  1,  1,  1],
        [ 1,  0,  1,  0,  1,  0],
        [ 0,  0,  1,  1,  0,  0],
        [ 0,  1,  1,  0,  0,  0]])

您还将看到,我正在使用更新版本的networkx生成稀疏矩阵,因此我添加了.todense()方法以获得密集(numpy)矩阵。

我有一个非常类似的问题,尽管它涉及生成关联矩阵。我在这里发布了我的问题:我有一个非常类似的问题,尽管它涉及到生成关联矩阵。我在这里发布了我的问题:我有一个非常类似的问题,尽管它涉及到生成关联矩阵。我在这里发布了我的问题:我有一个非常类似的问题,尽管它涉及到生成关联矩阵。我已经在这里发布了我的问题: