igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[igraph] Re: converting numpy adjacency matrices into igraph graph objec


From: Ali GÜRKAN
Subject: [igraph] Re: converting numpy adjacency matrices into igraph graph objects
Date: Thu, 26 Nov 2009 17:54:55 +0100

Hello again,
Thanks for the advice. It worked fine. I hope one day I can be expert enough too to make a contribution :)

Ali

2009/11/25 Tamas Nepusz <address@hidden>
Hi Ali,

Please stay on the mailing list, don't send your replies directly to me (it's better this way for archival purposes).

> Your  comments absolutely make sense. However it makes Pyhton crash. Do you know any way to handle this?
I guess you're running out of memory... a 6k x 6k adjacency matrix is large enough even for numpy (288 MB if we are assuming 8 bytes per element), and it has to be converted to a regular Python "list of lists", which can be much larger. A 6500x6500 matrix containing only zeros takes up around 1.6 GB when I convert it to Python's "list of lists" representation. So you will need some custom code to convert the matrix to an igraph graph. I'd try something along the following lines (not tested, may contain syntax errors, but it shows the general idea):

def numpy_array_to_igraph(a):
   edgelist = []
   for v1, row in enumerate(a):
       edgelist.extend(v2 for v2, element in row if element > 0)
   return Graph(edgelist, directed=True)

If you need an undirected graph, use directed=False and use "if element > 0 and v2 > v1" inside edgelist.extend to make sure you're not adding each edge twice.

--
Tamas




--
University of Naples Federico II
Dpt. Of Business and Managerial Engineering (DIEG)
Piazzale Tecchio 80, 80125 Naples (Italy)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]