来自 numpy 或 pandas 邻接矩阵的 igraph 图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29655111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
igraph Graph from numpy or pandas adjacency matrix
提问by LondonRob
I have an adjacency matrix stored as a pandas.DataFrame:
我有一个邻接矩阵存储为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.Graphfrom either the pandasor the numpyadjacency matrices. In an ideal world the nodes would be named as expected.
我想igraph.Graph从pandas或numpy邻接矩阵创建一个。在理想的世界中,节点将按预期命名。
Is this possible? The tutorialseems to be silent on the issue.
这可能吗?该教程似乎对这个问题保持沉默。
回答by RickardSjogren
In igraph you can use igraph.Graph.Adjacencyto 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.arrayor pd.DataFrame.
在 igraph 中,您可以使用igraph.Graph.Adjacency从邻接矩阵创建图形而无需使用zip. 当使用加权邻接矩阵并将其存储在 anp.array或中时,需要注意一些事项pd.DataFrame。
igraph.Graph.Adjacencycan't take annp.arrayas argument, but that is easily solved usingtolist.Integers in adjacency-matrix are interpreted as number of edges between nodes rather than weights, solved by using adjacency as boolean.
igraph.Graph.Adjacency不能将 annp.array作为参数,但使用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
You can reconstruct your adjacency dataframe using get_adjacencyby:
您可以使用get_adjacency以下方法重建邻接数据帧:
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
回答by ali_m
Strictly speaking, an adjacency matrixis boolean, with 1 indicating the presence of a connection and 0 indicating the absence. Since many of the values in your a_numpymatrix are > 1, I will assume that they correspond to edge weights in your graph.
严格来说,邻接矩阵是布尔值,1 表示存在连接,0 表示不存在。由于a_numpy矩阵中的许多值> 1,我将假设它们对应于图中的边权重。
import igraph
# get the row, col indices of the non-zero elements in your adjacency matrix
conn_indices = np.where(a_numpy)
# get the weights corresponding to these indices
weights = a_numpy[conn_indices]
# a sequence of (i, j) tuples, each corresponding to an edge from i -> j
edges = zip(*conn_indices)
# initialize the graph from the edge sequence
G = igraph.Graph(edges=edges, directed=True)
# assign node names and weights to be attributes of the vertices and edges
# respectively
G.vs['label'] = node_names
G.es['weight'] = weights
# I will also assign the weights to the 'width' attribute of the edges. this
# means that igraph.plot will set the line thicknesses according to the edge
# weights
G.es['width'] = weights
# plot the graph, just for fun
igraph.plot(G, layout="rt", labels=True, margin=80)



