# Getting adjacency matrix from graph and do matrix operations

**URL:** <https://igraph.discourse.group/t/getting-adjacency-matrix-from-graph-and-do-matrix-operations/1614>\
**Category:** Usage\
**Tags:** Python\
**Created:** [12 July 2023 20:35 UTC](https://igraph.discourse.group/t/getting-adjacency-matrix-from-graph-and-do-matrix-operations/1614 "2023-07-12T20:35:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaa](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@Aaa](https://igraph.discourse.group/u/Aaa)\
**Post date:** [12 July 2023 20:35 UTC](https://igraph.discourse.group/t/getting-adjacency-matrix-from-graph-and-do-matrix-operations/1614/1 "2023-07-12T20:35:01Z")

</div>

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

```python
import igraph
import numpy as np

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

```

which gives me the error

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

```

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

```python
Traceback (most recent call last):
  File "/Users/Name/Desktop/Folder/main.py", line 18, in <module>
    np.exp(Adj)
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?

Thanks!

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [12 July 2023 21:07 UTC](https://igraph.discourse.group/t/getting-adjacency-matrix-from-graph-and-do-matrix-operations/1614/2 "2023-07-12T21:07:18Z")

</div>

> [@Aaa](#):
>
> `Adj = np.array(sto.get_adjacency())`

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:

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

```

---

<div class="post-metadata">

**Author:** ![Aaa](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@Aaa](https://igraph.discourse.group/u/Aaa)\
**Post date:** [12 July 2023 21:21 UTC](https://igraph.discourse.group/t/getting-adjacency-matrix-from-graph-and-do-matrix-operations/1614/3 "2023-07-12T21:21:45Z")

</div>

Sorry, yes I meant to write Adj = np.array(g.get\_adjacency()).

And thanks! The code works now 🙂
