ubuntu_noob
ubuntu_noob

Reputation: 2365

networkx giving wrong adjacency matrix output

import networkx as nx
import pandas as pd

data1 = { 'node1': [1,1,1,2],
     'node2': [2,3,6,4],
     'weight': [1,1,1,1], }
df1 = pd.DataFrame(data1, columns = ['node1','node2','weight'])

df1.to_csv('training.csv')

df=pd.read_csv('training.csv')
G=nx.from_pandas_dataframe(df1,'node1','node2','weight')
print df1
Adjtraining = nx.adjacency_matrix(G)
print Adjtraining.todense()    

output:

[[0 1 1 0 1]
 [1 0 0 1 0]
 [1 0 0 0 0]
 [0 1 0 0 0]
 [1 0 0 0 0]]

but the actual output should be:

[[0 1 1 0 0 1]
 [1 0 0 1 0 0]
 [1 0 0 0 0 0]
 [0 1 0 0 0 0]
 [0 0 0 0 0 0]
 [1 0 0 0 0 0]]

This is because as we can see from the dataframe the nodes are 1 2 3 4 & 6.The node 5 is not there in the node list.But still it should be included in the adjacency matrix but it is ignored by the networkx.

Upvotes: 0

Views: 861

Answers (1)

michaelg
michaelg

Reputation: 954

The node 5 doesn't appear in the graph because it was instantiated by an edges list. You can still add it to the graph if you want it in the resulting adjacency matrix.

G = nx.from_pandas_dataframe(df1, 'node1','node2','weight')
G.add_node(5)
adj = nx.adjacency_matrix(G)
print(adj.todense())

[[0 1 1 1 0 0]  
 [1 0 0 0 1 0]  
 [1 0 0 0 0 0]  
 [1 0 0 0 0 0]  
 [0 1 0 0 0 0]  
 [0 0 0 0 0 0]]

The index of the matrix is:

print(G.nodes())
[1, 2, 3, 6, 4, 5]

Upvotes: 0

Related Questions