python - Plot NetworkX Graph from Adjacency Matrix in CSV file -


i have been battling problem little bit now, know simple - have little experience python or networkx. question simple, trying plot large dataset (about 200 rows/columns) of matrix looks this. first row , first column identical.

  a,b,c,d,e,f,g,h,i,j,k a,0,1,1,0,1,1,1,1,0,1,0 b,1,0,0,0,1,1,1,1,0,1,0 c,1,0,0,0,1,1,1,1,0,1,0 

it matrix showing how people connected, , all want import , plot csv file, it's corresponding labels in networkx.

i have file (people.csv), , looking @ previous answers here, seems best way putting data in array numpy.

there seems problem this:

import numpy np import networkx nx import matplotlib.pyplot plt numpy import genfromtxt import numpy np  mydata = genfromtxt('mouse.csv', delimiter=',') 

i following output:

file "/system/library/frameworks/python.framework/versions/2.7/extras/lib/python/numpy/lib/npyio.py", line 1272, in genfromtxt   fhd = iter(np.lib._datasource.open(fname, 'rbu')) file "/system/library/frameworks/python.framework/versions/2.7/extras/lib/python/numpy/lib/_datasource.py", line 145, in open   return ds.open(path, mode) file "/system/library/frameworks/python.framework/versions/2.7/extras/lib/python/numpy/lib/_datasource.py", line 472, in open   found = self._findfile(path) file "/system/library/frameworks/python.framework/versions/2.7/extras/lib/python/numpy/lib/_datasource.py", line 323, in _findfile   if self.exists(name): file "/system/library/frameworks/python.framework/versions/2.7/extras/lib/python/numpy/lib/_datasource.py", line 417, in exists   urllib2 import urlopen file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/urllib2.py", line 94, in <module>   import httplib file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/httplib.py", line 69, in <module>   array import array       file "/users/plosslab/documents/pythonstuff/array.py", line 4, in <module>       nameerror: name 'array' not defined 

i made small csv called mycsv.csv has following:

,a,b,c,d a,0,1,0,1 b,1,0,1,0 c,0,1,0,1 d,1,0,1,0 

you don't have ',' first character on first row, instead have space, if error on part let me know. general idea same. read in csv such:

from numpy import genfromtxt import numpy np mydata = genfromtxt('mycsv.csv', delimiter=',') print(mydata) print(type(mydata)) 

this prints:

[[ nan  nan  nan  nan  nan]  [ nan   0.   1.   0.   1.]  [ nan   1.   0.   1.   0.]  [ nan   0.   1.   0.   1.]  [ nan   1.   0.   1.   0.]] <type 'numpy.ndarray'> 

now have csv read in numpy array need extract adjacency matrix:

adjacency = mydata[1:,1:] print(adjacency) 

this prints:

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

you can slice numpy array needed if small example isn't yours.

to plot graph need import matplotlib , networkx:

import matplotlib.pyplot plt import networkx nx  def show_graph_with_labels(adjacency_matrix, mylabels):     rows, cols = np.where(adjacency_matrix == 1)     edges = zip(rows.tolist(), cols.tolist())     gr = nx.graph()     gr.add_edges_from(edges)     nx.draw(gr, node_size=500, labels=mylabels, with_labels=true)     plt.show()  show_graph_with_labels(adjacency, make_label_dict(get_labels('mycsv.csv'))) 

here's short tutorial on graphs python.

graph csv


Popular posts from this blog

debugging - Reference - What does this error mean in PHP? -

Perl "Out of memory!" when processing a large batch job -