Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

igraph Graph from numpy or pandas adjacency matrix

Tags:

I have an adjacency matrix stored as a pandas.DataFrame:

node_names = ['A', 'B', 'C'] a = pd.DataFrame([[1,2,3],[3,1,1],[4,0,2]],     index=node_names, columns=node_names) a_numpy = a.as_matrix() 

I'd like to create an igraph.Graph from either the pandas or the numpy adjacency matrices. In an ideal world the nodes would be named as expected.

Is this possible? The tutorial seems to be silent on the issue.

like image 258
LondonRob Avatar asked Apr 15 '15 15:04

LondonRob


People also ask

Which is better Igraph or NetworkX?

NetworkX is pure Python, well documented and handles changes to the network gracefully. iGraph is more performant in terms of speed and ram usage but less flexible for dynamic networks. iGraph is a C library with very smart indexing and storage approaches so you can load pretty large graphs in ram.

What is an adjacency matrix in python?

An adjacency matrix is a matrix that represents exactly which vertices/nodes in a graph have edges between them. It serves as a lookup table, where a value of 1 represents an edge that exists and a 0 represents an edge that does not exist. The indices of the matrix model the nodes.

What do you mean by adjacency matrix?

In graph theory, an adjacency matrix is nothing but a square matrix utilised to describe a finite graph. The components of the matrix express whether the pairs of a finite set of vertices (also called nodes) are adjacent in the graph or not.


1 Answers

In igraph you can use igraph.Graph.Adjacency to create a graph from an adjacency matrix without having to use zip. There are some things to be aware of when a weighted adjacency matrix is used and stored in a np.array or pd.DataFrame.

  • igraph.Graph.Adjacency can't take an np.array as argument, but that is easily solved using tolist.

  • Integers in adjacency-matrix are interpreted as number of edges between nodes rather than weights, solved by using adjacency as boolean.

An example of how to do it:

import igraph import pandas as pd  node_names = ['A', 'B', 'C'] a = pd.DataFrame([[1,2,3],[3,1,1],[4,0,2]], index=node_names, columns=node_names)  # Get the values as np.array, it's more convenenient. A = a.values  # Create graph, A.astype(bool).tolist() or (A / A).tolist() can also be used. g = igraph.Graph.Adjacency((A > 0).tolist())  # Add edge weights and node labels. g.es['weight'] = A[A.nonzero()] g.vs['label'] = node_names  # or a.index/a.columns 

You can reconstruct your adjacency dataframe using get_adjacency by:

df_from_g = pd.DataFrame(g.get_adjacency(attribute='weight').data,                          columns=g.vs['label'], index=g.vs['label']) (df_from_g == a).all().all()  # --> True 
like image 195
RickardSjogren Avatar answered Sep 20 '22 18:09

RickardSjogren