# How to convert a ndarray to an igraph's graph quickly?

**URL:** <https://igraph.discourse.group/t/how-to-convert-a-ndarray-to-an-igraphs-graph-quickly/1478>\
**Category:** Usage\
**Tags:** Python\
**Created:** [2 March 2023 09:38 UTC](https://igraph.discourse.group/t/how-to-convert-a-ndarray-to-an-igraphs-graph-quickly/1478 "2023-03-02T09:38:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![forestbat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/forestbat/32/902_2.png) [@forestbat](https://igraph.discourse.group/u/forestbat)\
**Post date:** [2 March 2023 09:38 UTC](https://igraph.discourse.group/t/how-to-convert-a-ndarray-to-an-igraphs-graph-quickly/1478/1 "2023-03-02T09:38:51Z")

</div>

I have a ndarray which calls `pairs` looks like this:

 ![图片](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/a1cace8802ecde9333bc22329d8798305846c639.png)  
you can see every elements in the ndarray is an array which length is 2.  
Now I want to convert `pairs` to a list of tuples and add the list to an graph，but I can’t find a fast method to complete this.  
I have used this code to convert：

```auto
for i in range(0, len(pairs)):
        pairs[i] = tuple(pairs[i])

```

but there is no any change in `pairs` ndarray, so how to change my code? Thanks for very much.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [3 March 2023 15:40 UTC](https://igraph.discourse.group/t/how-to-convert-a-ndarray-to-an-igraphs-graph-quickly/1478/2 "2023-03-03T15:40:36Z")

</div>

You can use numpy arrays directly. `Graph(pairs)` should work for creating a graph. If it does not, please show a complete minimal example, and include your numpy and igraph versions.

---

<div class="post-metadata">

**Author:** ![forestbat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/forestbat/32/902_2.png) [@forestbat](https://igraph.discourse.group/u/forestbat)\
**Post date:** [6 March 2023 13:42 UTC](https://igraph.discourse.group/t/how-to-convert-a-ndarray-to-an-igraphs-graph-quickly/1478/3 "2023-03-06T13:42:08Z")

</div>

This is my crash report：  
ig\_path\_test.py:58: in build\_graph  
total\_graph.add\_edges(pairs)

* * *

graph = \<igraph.Graph object at 0x000002C86F83BC40\>  
es = array([[1, 4],  
[1, 2],  
[2, 4],  
…,  
[77912, 77911],  
[77912, 48205],  
[77912, 48130]], dtype=int64)  
attributes = None

```
def _add_edges(graph, es, attributes=None):
    """Adds some edges to the graph.

    @param es: the list of edges to be added. Every edge is represented
      with a tuple containing the vertex IDs or names of the two
      endpoints. Vertices are enumerated from zero.
    @param attributes: dict of sequences, all of length equal to the
      number of edges to be added, containing the attributes of the new
      edges.
    """
    eid = graph.ecount()

```

> ```
> res = GraphBase.add_edges(graph, es)
> 
> ```

```auto
E igraph._igraph.InternalError: Error at src/graph/type_indexededgelist.c:261: Out-of-range vertex IDs when adding edges. -- Invalid vertex ID

```

---

<div class="post-metadata">

**Author:** ![forestbat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/forestbat/32/902_2.png) [@forestbat](https://igraph.discourse.group/u/forestbat)\
**Post date:** [6 March 2023 13:45 UTC](https://igraph.discourse.group/t/how-to-convert-a-ndarray-to-an-igraphs-graph-quickly/1478/4 "2023-03-06T13:45:37Z")

</div>

Oh，the method can pass the test：  
`total_graph = ig.Graph(pairs, directed=True)`  
But why `add_edges` fails？

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [6 March 2023 14:15 UTC](https://igraph.discourse.group/t/how-to-convert-a-ndarray-to-an-igraphs-graph-quickly/1478/5 "2023-03-06T14:15:00Z")

</div>

> [@forestbat](#):
>
> `igraph._igraph.InternalError: Error at src/graph/type_indexededgelist.c:261: Out-of-range vertex IDs when adding edges.`

This error will occur when you pass invalid vertex IDs to `add_edges()`. Valid IDs are between `0` and `g.vcount() - 1` (inclusive).

If you want to create a graph from an edge list, do not use `add_edges()`. Use the `Graph` constructor instead, which will automatically determine the appropriate vertex count.

* * *

If there are further problems, please include a short but _complete_ reproducer according to the guidelines here: [How to create a Minimal, Reproducible Example - Help Center - Stack Overflow](https://stackoverflow.com/help/minimal-reproducible-example) Be sure to mention your igraph version (ideally use the latest version).

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [6 March 2023 14:18 UTC](https://igraph.discourse.group/t/how-to-convert-a-ndarray-to-an-igraphs-graph-quickly/1478/6 "2023-03-06T14:18:11Z")

</div>

Simple way to create graph from edge list:

```auto
In [4]: g1 = ig.Graph([(0,1), (1,2)])

```

You can also do this:

```auto
In [5]: g2 = ig.Graph()

```

This graph has no vertices:

```auto
In [6]: g2.vcount()
Out[6]: 0

```

Thus e.g. vertex ID `2` is not valid.

```auto
In [7]: g2.add_edges([(0,1), (1,2)])

InternalError: Error at src/graph/type_indexededgelist.c:261: Out-of-range vertex IDs when adding edges. -- Invalid vertex ID

```

You need to add at least 3 vertices in order for vertex ID `2` to be valid.

```auto
In [8]: g2.add_vertices(3)

In [9]: g2.add_edges([(0,1), (1,2)])

```
