从Network Graph(反之亦然)有效创建邻接矩阵Python NetworkX [英] Efficiently create adjacency matrix from network graph (vice versa) Python NetworkX
问题描述
我试图创建网络图并从中生成稀疏矩阵。从wikipedia Laplacian矩阵
例如,我决定尝试使用 networkx
$ b
如何可以有效地将 adjacency matrix
和a 网络图
?
例如,如果我有一个网络图,我如何能够快速将其转换为邻接矩阵,并且如果我有一个邻接图,我怎样才能有效地将其转换为网络图。
以下是我的代码,我觉得对于大型网络来说效率很低。
#!/ usr / bin / python
$如何将图转换为邻接矩阵:
将networkx导入为nx
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
import pandas as pd
%matplotlib inline
#相邻矩阵
adj_matrix = np.matrix([[0,1,0,0,1,0],[1,0,1,0,1,0],[0,1,0,1,0, 0],[0,0,1,0,1,1],[1,1,0,1,0,0],[0,0,0,1,0,0]])
adj_sparse = sp.sparse.coo_matrix(adj_matrix,dtype = np.int8)
labels =范围(1,7)
DF_adj = pd.DataFrame(adj_sparse.toarray(),index = labels,columns =标签)
print DF_adj
#1 2 3 4 5 6
#1 0 1 0 0 1 0
#2 1 0 1 0 1 0
#3 0 1 0 1 0 0
#4 0 0 1 0 1 1
#5 1 1 0 1 0 0
#6 0 0 0 1 0 0
$网络图
G = nx.Graph()
G.add_nodes_from(标签)
#对于范围内的连接节点
(DF_adj.shape对于范围内的j(DF_adj.shape [1]),
col_label = DF_adj.columns [i]
:
row_label = DF_adj.index [j]
node = DF_adj.iloc [i,j]
if node == 1:
G.add_edge(col_label,row_label)
#Draw graph
nx.draw(G,with_labels = True)
#DRAWN图匹配来自WIKI的图
#Recreate邻接矩阵
DF_re = pd.DataFrame (np.zeros([len(G.nodes()),len(G.nodes())]),index = G.nodes(),columns = G.nodes())
for col_label,row_label在G.edges()中:
DF_re.loc [col_label,row_label] = 1
DF_re.loc [row_label,col_label] = 1
print G.edges()
# [(1,2),(1,5),(2,3),(2,5),(3,4),(4,5),(4,6)]
print DF_re
#1 2 3 4 5 6
#1 0 1 0 0 1 0
#2 1 0 1 0 1 0
#3 0 1 0 1 0 0
#4 0 0 1 0 1 1
#5 1 1 0 1 0 0
#6 0 0 0 1 0 0
$ $ p>将scipy导入为sp
将networkx导入为nx
G = nx.fast_gnp_rando m_graph(100,0.04)
adj_matrix = nx.adjacency_matrix(G)
这里是< a href =https://networkx.github.io/documentation/latest/reference/generated/networkx.linalg.graphmatrix.adjacency_matrix.html =nofollow>文档。
从邻接矩阵到图:
H = nx.Graph(adj_matrix)#if它是指示,使用H = nx.DiGraph(adj_matrix)
以下是文件。
I'm trying to get into creating network graphs and generating sparse matrices from them. From the wikipedia Laplacian matrix
example, I decided to try and recreate the following network graph using networkx
How can one EFFICIENTLY convert between an adjacency matrix
and a network graph
?
For example, if I have a network graph, how can I quickly convert it to an adjacency matrix and if I have an adjacency graph how can I efficiently convert it to a network graph.
Below is my code for doing it and I feel like it's pretty inefficient for larger networks.
#!/usr/bin/python
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
import pandas as pd
%matplotlib inline
#Adjacent matrix
adj_matrix = np.matrix([[0,1,0,0,1,0],[1,0,1,0,1,0],[0,1,0,1,0,0],[0,0,1,0,1,1],[1,1,0,1,0,0],[0,0,0,1,0,0]])
adj_sparse = sp.sparse.coo_matrix(adj_matrix, dtype=np.int8)
labels = range(1,7)
DF_adj = pd.DataFrame(adj_sparse.toarray(),index=labels,columns=labels)
print DF_adj
# 1 2 3 4 5 6
#1 0 1 0 0 1 0
#2 1 0 1 0 1 0
#3 0 1 0 1 0 0
#4 0 0 1 0 1 1
#5 1 1 0 1 0 0
#6 0 0 0 1 0 0
#Network graph
G = nx.Graph()
G.add_nodes_from(labels)
#Connect nodes
for i in range(DF_adj.shape[0]):
col_label = DF_adj.columns[i]
for j in range(DF_adj.shape[1]):
row_label = DF_adj.index[j]
node = DF_adj.iloc[i,j]
if node == 1:
G.add_edge(col_label,row_label)
#Draw graph
nx.draw(G,with_labels = True)
#DRAWN GRAPH MATCHES THE GRAPH FROM WIKI
#Recreate adjacency matrix
DF_re = pd.DataFrame(np.zeros([len(G.nodes()),len(G.nodes())]),index=G.nodes(),columns=G.nodes())
for col_label,row_label in G.edges():
DF_re.loc[col_label,row_label] = 1
DF_re.loc[row_label,col_label] = 1
print G.edges()
#[(1, 2), (1, 5), (2, 3), (2, 5), (3, 4), (4, 5), (4, 6)]
print DF_re
# 1 2 3 4 5 6
#1 0 1 0 0 1 0
#2 1 0 1 0 1 0
#3 0 1 0 1 0 0
#4 0 0 1 0 1 1
#5 1 1 0 1 0 0
#6 0 0 0 1 0 0
How to convert from graph to adjacency matrix:
import scipy as sp
import networkx as nx
G=nx.fast_gnp_random_graph(100,0.04)
adj_matrix = nx.adjacency_matrix(G)
Here's the documentation.
And from adjacency matrix to graph:
H=nx.Graph(adj_matrix) #if it's directed, use H=nx.DiGraph(adj_matrix)
Here's the documentation.
这篇关于从Network Graph(反之亦然)有效创建邻接矩阵Python NetworkX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!