来自 numpy 或 pandas 邻接矩阵的 igraph 图

igraph Graph from numpy or pandas adjacency matrix

我有一个存储为 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()

我想从 pandasnumpy 邻接矩阵创建一个 igraph.Graph。在理想的世界中,节点将按预期命名。

这可能吗? The tutorial 似乎对这个问题保持沉默。

严格来说,adjacency matrix 是布尔值,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)

在 igraph 中,您可以使用 igraph.Graph.Adjacency 从邻接矩阵创建图形,而无需使用 zip。当使用加权邻接矩阵并将其存储在 np.arraypd.DataFrame.

中时,需要注意一些事项
  • igraph.Graph.Adjacency 不能将 np.array 作为参数,但是使用 tolist.

  • 很容易解决
  • 邻接矩阵中的整数被解释为节点之间的边数而不是权重,通过使用邻接作为布尔值来解决。

操作示例:

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

您可以使用 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

with igraph.Graph.Weighted_Adjacency 作为

g = igraph.Graph.Weighted_Adjacency(a.to_numpy().tolist())

pandas.DataFrame.as_matrixhas been deprecated, 所以 pandas.DataFrame.to_numpy 应该改用。 此外,a.to_numpy() 给出的 numpy.ndarray 在传递给 Weighted_Adjacency.

之前必须转换为带有 tolist() 的列表

节点名称可以存储为另一个属性

g.vs['name'] = node_names