Getting adjacency matrix from graph and do matrix operations

I’m trying to get an adjacency matrix from a graph and do matrix operations. My code is as follows:

import igraph
import numpy as np

g = igraph.Graph.GRG(10,0.5)
Adj = np.array(sto.get_adjacency())

which gives me the error

AttributeError: 'Matrix' object has no attribute 'exp'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/Name/Desktop/Folder/", line 18, in <module>
TypeError: loop of ufunc does not support argument 0 of type Matrix which has no callable exp method

I thought it’s because the type of argument is wrong. However, I checked that the type(Adj) is the same as type(np.identity(3)), and np.exp(np.identity(3)) works just fine. Any thoughts?


You probably meant to write Adj = np.array(g.get_adjacency()) here. However, that doesn’t solve the problem. g.get_adjacency() returns a igraph.Matrix type. The np.array call just wraps that igraph.Matrix in a numpy array. Instead, you want to create a numpy Matrix. For this, you need to get the underlying data of the igraph.Matrix. Hence, you should replace this by:

Adj = np.array(g.get_adjacency().data)

Sorry, yes I meant to write Adj = np.array(g.get_adjacency()).

And thanks! The code works now :slight_smile: