添加两个矩阵并维护结构
Adding two matrices and maintain the structure
我想add/merge两个邻接矩阵形式的图并处理结构。
第一张图是这样的:
相关的邻接矩阵为:
0. 1. 1. 1.
1. 0. 1. 1.
1. 1. 0. 1.
1. 1. 1. 0.
第二张图是这样的:
相关的邻接矩阵为:
[[0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 1. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 1. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 1. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 1. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 0. 0. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0.]]
现在我想,我可以填充第一个矩阵并将第一个矩阵简单地添加到第二个矩阵。但是结果不是我想要的。
所以我的问题是,如何 merge/add 两个邻接矩阵并采用 care/maintain 它们的结构...?
我想要的结果应该是这样的:
任何想法都会有所帮助:)。
我想这就是您想要的。诀窍是使用 nx.compose
将两个图合并在一起。
设置第一个图表:
import networkx as nx
adjmat1 = np.array([[0., 1., 1., 1.],
[1., 0., 1., 1.],
[1., 1., 0., 1.],
[1., 1., 1., 0.]])
adjmat1_df = pd.DataFrame(adjmat1)
node_labels = [(1,1),(1,2),(2,1),(2,2)]
adjmat1_df.columns = node_labels
adjmat1_df.index = node_labels
G1 = nx.from_pandas_adjacency(adjmat1_df)
pos = {(x,y):(y,-x) for x,y in G1.nodes()}
nx.draw(G1, pos=pos,
node_color='lightgreen',
with_labels=True,
node_size=600)
设置第二个图表(我已经进行了快速网格设置,但您可以像第一步一样使用第二个邻接矩阵创建图表):
# Following @yatu's SO answer for plotting
G2 = nx.grid_2d_graph(4,4)
plt.figure(figsize=(4,4))
pos = {(x,y):(y,-x) for x,y in G2.nodes()}
nx.draw(G2, pos=pos,
node_color='lightgreen',
with_labels=True,
node_size=600)
使用nx.compose
组合图表:
H = nx.compose(G1,G2)
pos = {(x,y):(y,-x) for x,y in H.nodes()}
nx.draw(H, pos=pos,
node_color='lightgreen',
with_labels=True,
node_size=600)
我想add/merge两个邻接矩阵形式的图并处理结构。
第一张图是这样的:
相关的邻接矩阵为:
0. 1. 1. 1.
1. 0. 1. 1.
1. 1. 0. 1.
1. 1. 1. 0.
第二张图是这样的:
相关的邻接矩阵为:
[[0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 1. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 1. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 1. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 1. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 0. 0. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0.]]
现在我想,我可以填充第一个矩阵并将第一个矩阵简单地添加到第二个矩阵。但是结果不是我想要的。
所以我的问题是,如何 merge/add 两个邻接矩阵并采用 care/maintain 它们的结构...?
我想要的结果应该是这样的:
任何想法都会有所帮助:)。
我想这就是您想要的。诀窍是使用 nx.compose
将两个图合并在一起。
设置第一个图表:
import networkx as nx
adjmat1 = np.array([[0., 1., 1., 1.],
[1., 0., 1., 1.],
[1., 1., 0., 1.],
[1., 1., 1., 0.]])
adjmat1_df = pd.DataFrame(adjmat1)
node_labels = [(1,1),(1,2),(2,1),(2,2)]
adjmat1_df.columns = node_labels
adjmat1_df.index = node_labels
G1 = nx.from_pandas_adjacency(adjmat1_df)
pos = {(x,y):(y,-x) for x,y in G1.nodes()}
nx.draw(G1, pos=pos,
node_color='lightgreen',
with_labels=True,
node_size=600)
设置第二个图表(我已经进行了快速网格设置,但您可以像第一步一样使用第二个邻接矩阵创建图表):
# Following @yatu's SO answer for plotting
G2 = nx.grid_2d_graph(4,4)
plt.figure(figsize=(4,4))
pos = {(x,y):(y,-x) for x,y in G2.nodes()}
nx.draw(G2, pos=pos,
node_color='lightgreen',
with_labels=True,
node_size=600)
使用nx.compose
组合图表:
H = nx.compose(G1,G2)
pos = {(x,y):(y,-x) for x,y in H.nodes()}
nx.draw(H, pos=pos,
node_color='lightgreen',
with_labels=True,
node_size=600)