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: Tamas Nepusz
Subject: [igraph] Re: converting numpy adjacency matrices into igraph graph objects
Date: Wed, 25 Nov 2009 20:20:24 +0000

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





reply via email to

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