Reputation: 31
Lets say I have a numpy array
arr = np.array([1,2,3,4,5,6,7])
how would I go about turning this array into a graph with igraph.
Upvotes: 1
Views: 563
Reputation: 1244
In igraph you can use igraph.Graph.Adjacency to create a graph from an adjacency matrix without having to use zip.
know that igraph.Graph.Adjacency can't take an np.array as argument, but that is easily solved using tolist. 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
Try it with your own values of vertices and edges since you didn't specify enough information.
Upvotes: 1