从具有行和列标题的csv文件读取networkx图 [英] Reading a networkx graph from a csv file with row and column header
问题描述
我有一个CSV文件,表示图形的邻接矩阵。然而,文件具有作为第一行的节点的标签,并且作为第一列也是节点的标签。我如何读取这个文件到 networkx
图形对象?有没有一个整洁的pythonic的方式来做它没有黑客?
I have a CSV file that represents the adjacency matrix of a graph. However the file has as the first row the labels of the nodes and as the first column also the labels of the nodes. How can I read this file into a networkx
graph object? Is there a neat pythonic way to do it without hacking around?
我的试用到目前为止:
x = np.loadtxt('file.mtx', delimiter='\t', dtype=np.str)
row_headers = x[0,:]
col_headers = x[:,0]
A = x[1:, 1:]
A = np.array(A, dtype='int')
但是这当然不能解决问题,因为我需要在图形创建的节点的标签。
But of course this doesn't solve the problem since I need the labels for the nodes in the graph creation.
数据示例:
Attribute,A,B,C
A,0,1,1
B,1,0,0
C,1,0,0
Tab是分隔符,
推荐答案
您可以将数据读入结构化数组。标签可以从 x.dtype.names
获得,然后可以使用 nx.from_numpy_matrix
生成networkx图,
You could read the data into a structured array. The labels can be obtained from x.dtype.names
, and then the networkx graph can be generated using nx.from_numpy_matrix
:
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
# read the first line to determine the number of columns
with open('file.mtx', 'rb') as f:
ncols = len(next(f).split('\t'))
x = np.genfromtxt('file.mtx', delimiter='\t', dtype=None, names=True,
usecols=range(1,ncols) # skip the first column
)
labels = x.dtype.names
# y is a view of x, so it will not require much additional memory
y = x.view(dtype=('int', len(x.dtype)))
G = nx.from_numpy_matrix(y)
G = nx.relabel_nodes(G, dict(zip(range(ncols-1), labels)))
print(G.edges(data=True))
# [('A', 'C', {'weight': 1}), ('A', 'B', {'weight': 1})]
nx.from_numpy_matrix
有一个 create_using
参数,可以用来指定networkx的类型您要创建的图形。例如,
The nx.from_numpy_matrix
has a create_using
parameter you can use to specify the type of networkx Graph you wish to create. For example,
G = nx.from_numpy_matrix(y, create_using=nx.DiGraph())
使 G
a DiGraph
。
这篇关于从具有行和列标题的csv文件读取networkx图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!