与其查找除您的数据以外的函数,请将数据处理为正确的格式。
假数据f
模拟文件对象。对于Python 3.6使用io.StringIO
。
data = '''0 1
0 2
0 31
0 73
1 3
1 56
2 10'''
f = io.BytesIO(data)
读取数据的每一行,并将其加工成边缘的与所述形式(node1, node1)
列表。
edges = []
for line in f:
line = line.strip()
(node1, node2) = map(int, line.split())
edges.append((node1,node2))
找到最高点数,创建基于最高节点数量方 numpy的ndarray。您需要了解您的节点编号 - 是否基于零?
N = max(x for edge in edges for x in edge)
G = np.zeros((N+1,N+1), dtype = np.int64)
遍历边缘与边缘权重分配给所述图形
for row, column in edges:
G[row,column] = 1
这里是一个解决方案利用的numpy integer array indexing。
z = np.genfromtxt(f, dtype = np.int64)
n = z.max() + 1
g = np.zeros((n,n), dtype = np.int64)
rows, columns = z.T
g[rows, columns] = 1
当然这两个的假设所有的边缘的权重相等。
请参阅Graph Representations在scipy文档。我无法尝试此图表来查看它是否有效,我得到一个导入错误csgraph
- 可能需要更新。
看起来您需要将您的节点/边数据集处理为所需的格式 - 从文档,[图表复制](https://docs.scipy.org/doc/scipy/reference/sparse.csgraph.html#图表表示) – wwii