與其查找除您的數據以外的函數,請將數據處理爲正確的格式。
假數據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