NetworkX : When I add 'weight' to some node I can not generate adjacency_matrix()?
Question:
The moment I add ‘weight’ to a node, I can no longer generate adjacency_matrix() ? Any ideas of how to still be able to generate it ?
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'
the same goes for :
In [79]: nx.adjacency_matrix(g2,weight='weight')
Answers:
You are close – assign the node weight to g2.nodes[2]['weight']
and it will work.
Note though that the node weights do not appear in the adjacency matrix. It is the edge weights that are assigned there. For example
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.nodes[2]['weight']=7
In [5]: g2.nodes
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.edges[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]])
Also you will see I am using a newer version of networkx that generates sparse matrices so I have added the .todense() method to get a dense (numpy) matrix. In older versions use "node" and "edge".
The moment I add ‘weight’ to a node, I can no longer generate adjacency_matrix() ? Any ideas of how to still be able to generate it ?
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'
the same goes for :
In [79]: nx.adjacency_matrix(g2,weight='weight')
You are close – assign the node weight to g2.nodes[2]['weight']
and it will work.
Note though that the node weights do not appear in the adjacency matrix. It is the edge weights that are assigned there. For example
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.nodes[2]['weight']=7
In [5]: g2.nodes
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.edges[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]])
Also you will see I am using a newer version of networkx that generates sparse matrices so I have added the .todense() method to get a dense (numpy) matrix. In older versions use "node" and "edge".