Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

turning igraph adjacency matrix into numpy array

By writing

import igraph
g = igraph.Graph()
g.add_vertices(6)
g.add_edges([(0,1),(0,3),(0,4),(0,5),(1,2),(2,4),(2,5),(3,0),(3,2),(3,5),(4,5),(3,3)])
A=g.get_adjacency()

I get the adjacency matrix of graph g, as a Matrix object. I want to calculate its eigenvalues by using, for example, numpy.linalg.eigvals(). This method takes a numpy array object as argument. How do I convert a Matrix object into a numpy array object? I tried by using

X=numpy.matrix(A)

but it produced some mixture of the two and eigenvalues could not be calculated.

like image 989
yannis Avatar asked Jan 09 '23 23:01

yannis


1 Answers

According to the documentation of iGraph's matrix class, you could retrieve the data as a list of lists and then convert easily to a numpy ndarray:

A = g.get_adjacency()
A = np.array(A.data)
like image 128
Oliver W. Avatar answered Jan 16 '23 22:01

Oliver W.