簡體   English   中英

Python numpy - 矩陣的字符串表示為二維 numpy 數組,同時保持格式化並將其提供給 NetworkX

[英]Python numpy - string representation of a matrix to 2D numpy array while keeping formatting and feed it to NetworkX

我的代碼需要一些幫助。 我有以下格式的txt文件(2個樣本):

wwwwwwwwwwwwwwwwwwwwwwwwww
w...o.xx.o......o..xoxx..w
w...oooooo........o..o...w
w....xxx.........o.oxoo.ow
wx...............oxo...oow
wwwwwwwwww........o...wxxw
wb ...co..............wxxw
w  ........Ao....o....wxxw
wooo............. ....w..w
w......x....wwwwx x.oow..w
wc  .....x..ooxxo ....w..w
w   ..E..........b     ..w
wwwwwwwwwwwwwwwwwwwwwwwwww

wwwwwwwwwwwww
w........w..w
w...1.......w
w...A.1.w.0ww
www.w1..wwwww
w.......w.0.w
w.1........ww
w..........ww
wwwwwwwwwwwww

我想要做的是讀取文件夾中的所有文件(可能有大約 10 個這些 txt 文件),將所有字符更改為 0,除了字符“w”和“A”,這些將是 1 和 2。

我設法弄清楚了這部分,這是我的部分解決方案:

import glob
import os
import re
import numpy as np
import ast
import networkx as nx
def create_matrix_from_layout(folder):
    files = glob.glob(folder)
    file_set = {}
    for f in files:
        fname = os.path.basename(f)
        with open(f, "r") as tmp:
            tmp_data = tmp.read()
        tmp_data = re.sub(r"[^wA\n]", r"0", tmp_data)
        tmp_data = tmp_data.replace("w", "1").replace("A", "2")
        arr = np.array(tmp_data)
        #print(arr.ndim)
        print(arr)
        #G = nx.from_numpy_matrix(arr)
        #print(G)

create_matrix_from_layout("layouts/*.txt")

這是 output(樣品):

111111111111111111111111
111100001000110000000111
100000101000000011000011
100000000011002011100001
101111011111000011000111
100000000100000000000011
110001000000110001110001
110000110001111000010001
111000000000000001000001
111111000000111111000011

一切看起來都很好,但問題就從這里開始了。 我需要將它提供給 NetworkX 並創建一個由頂點(1)和連接它們的邊組成的圖。

顯然,我的數據結構不是 NetworkX 想要使用的,但我還沒有找到更好的方法。

NetworkXError: Input array must be 2D, not 0

任何幫助,將不勝感激。 謝謝你。

您可以創建一個網格圖,然后刪除所有為零的節點。 通過這種方式,您將有一個圖表,其中矩陣中的每個“1”都有一個節點,並且相鄰的 1 是連接的。

from itertools import product

import networkx as nx
import numpy as np

coor = np.array(list(product(*map(range, arr.shape))))
G = nx.grid_2d_graph(*arr.shape)
G.remove_nodes_from(map(tuple, coor[arr.flatten() == 0]))

這里假設arr是一個包含 1 或 0 的 numpy 數組。這是一個示例:

>>> arr
array([[1, 1, 1, 1, 1],
       [1, 0, 0, 1, 0],
       [0, 0, 1, 1, 1],
       [1, 1, 1, 1, 1]])
>>> coor = np.array(list(product(*map(range, arr.shape))))
>>> G = nx.grid_2d_graph(*arr.shape)
>>> G.remove_nodes_from(map(tuple, coor[arr.flatten() == 0]))
>>> G.nodes
NodeView(((0, 0), (0, 1), (0, 2), (0, 3), (0, 4),
          (1, 0), (1, 3), (2, 2), (2, 3), (2, 4),
          (3, 0), (3, 1), (3, 2), (3, 3), (3, 4)))
>>> G.edges
EdgeView([((0, 0), (1, 0)), ((0, 0), (0, 1)), ((0, 1), (0, 2)),
          ((0, 2), (0, 3)), ((0, 3), (1, 3)), ((0, 3), (0, 4)),
          ((1, 3), (2, 3)), ((2, 2), (3, 2)), ((2, 2), (2, 3)),
          ((2, 3), (3, 3)), ((2, 3), (2, 4)), ((2, 4), (3, 4)),
          ((3, 0), (3, 1)), ((3, 1), (3, 2)), ((3, 2), (3, 3)),
          ((3, 3), (3, 4))])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM