Mattematics
Mattematics

Reputation: 35

Constructing a graph from adjacency matrix in igraph

So I'm trying to construct a graph using igraph and an adjacency matrix that I have. It's a symmetrical matrix with ones where there should be an edge between two nodes and zeros where there is no connection. I'm using the code below to generate it:

g = igraph.Graph.Adjacency(adjacency)
igraph.plot(g, layout = 'kk')

My adjacency matrix is type

numpy.matrixlib.defmatrix.matrix

But I tried to convert it to a list and an array and still get the same error:

TypeError: Error while converting adjacency matrix

Any ideas? Thanks

Upvotes: 0

Views: 1924

Answers (1)

Nick Matteo
Nick Matteo

Reputation: 4553

The Adjacency method of igraph.Graph expects a matrix of the type igraph.datatypes.Matrix, not a numpy matrix.

igraph will convert a list of lists to a matrix. Try using

g = igraph.Graph.Adjacency(adjacency.astype(bool).tolist())

where adjacency is your numpy matrix of zeros and ones.

Upvotes: 2

Related Questions