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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With