来自numpy或pandas邻接矩阵的igraph图 [英] igraph Graph from numpy or pandas adjacency matrix

查看:369
本文介绍了来自numpy或pandas邻接矩阵的igraph图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储为pandas.DataFrame的邻接矩阵:

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()

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

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.

推荐答案

在igraph中,您可以使用 igraph.Graph.Adjacency 可以根据邻接矩阵创建图,而不必使用zip.使用加权邻接矩阵并将其存储在np.arraypd.DataFrame中时,需要注意一些事情.

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不能使用np.array作为参数,但是使用

  • 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.

操作方法示例:

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 通过以下方式重建邻接数据框:

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

这篇关于来自numpy或pandas邻接矩阵的igraph图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆