1 简介
networkx是一个用Python语言开发的图论与复杂网络建模工具,内置了常用的图与复杂网络分析算法,可以方便的进行复杂网络数据分析、仿真建模等工作。
利用networkx可以以标准化和非标准化的数据格式存储网络、生成多种随机网络和经典网络、分析网络结构、建立网络模型、设计新的网络算法、进行网络绘制等。
networkx支持创建简单无向图、有向图和多重图(multigraph);内置许多标准的图论算法,节点可为任意数据;支持任意的边值维度,功能丰富,简单易用。
networkx以图(graph)为基本数据结构。图既可以由程序生成,也可以来自在线数据源,还可以从文件与数据库中读取。
安装
安装的话,跟其他包的安装差不多,用的是anaconda就不用装了。其他就用pip install networkx。
查看版本:
1>>> import networkx
2>>> networkx.__version__
3'1.11'
升级
1pip install
下面配合使用的一些库,可以选择性安装:
后面可能用到pygraphviz,安装方法如下(亲测有效):
1sudo apt-get install graphviz
2sudo apt-get install graphviz libgraphviz-dev pkg-config
3sudo apt-get install python-pip python-virtualenv
4pip install pygraphviz
windows的安装参考这篇博客:https://blog.csdn.net/fadai1993/article/details/82491657#2____linux_9
安装cv2:
1pip install opencv-python
2pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
支持四种图
Graph:无多重边无向图
DiGraph:无多重边有向图
MultiGraph:有多重边无向图
MultiDiGraph:有多重边有向图
空图对象的创建方式
1import networkx as nx
2G=nx.Graph()
3G=nx.DiGraph()
4G=nx.MultiGraph()
5G=nx.MultiDiGraph()
6G.clear()
绘制网络图基本流程
最基本画图程序
1import import networkx as nx
2import matplotlib.pyplot as plt
3G = nx.random_graphs.barabasi_albert_graph(100,1)
4nx.draw(G)
5plt.savefig("ba.png")
6plt.show()
networkx 提供画图的函数
1draw(G,[pos,ax,hold])
2draw_networkx(G,[pos,with_labels])
3draw_networkx_nodes(G,pos,[nodelist])绘制网络G的节点图
4draw_networkx_edges(G,pos[edgelist])绘制网络G的边图
5draw_networkx_edge_labels(G, pos[, …]) 绘制网络G的边图,边有label
6—有layout 布局画图函数的分界线—
7draw_circular(G, **kwargs) Draw the graph G with a circular layout.
8draw_random(G, **kwargs) Draw the graph G with a random layout.
9draw_spectral(G, **kwargs)Draw the graph G with a spectral layout.
10draw_spring(G, **kwargs)Draw the graph G with a spring layout.
11draw_shell(G, **kwargs) Draw networkx graph with shell layout.
12draw_graphviz(G[, prog])Draw networkx graph with graphviz layout.
networkx 画图函数里的一些参数
pos(dictionary, optional): 图像的布局,可选择参数;如果是字典元素,则节点是关键字,位置是对应的值。如果没有指明,则会是spring的布局;也可以使用其他类型的布局,具体可以查阅networkx.layout
arrows :布尔值,默认True; 对于有向图,如果是True则会画出箭头
with_labels: 节点是否带标签(默认为True)
ax:坐标设置,可选择参数;依照设置好的Matplotlib坐标画图
nodelist:一个列表,默认G.nodes(); 给定节点
edgelist:一个列表,默认G.edges();给定边
node_size: 指定节点的尺寸大小(默认是300,单位未知,就是上图中那么大的点)
node_color: 指定节点的颜色 (默认是红色,可以用字符串简单标识颜色,例如’r’为红色,'b’为绿色等,具体可查看手册),用“数据字典”赋值的时候必须对字典取值(.values())后再赋值
node_shape: 节点的形状(默认是圆形,用字符串’o’标识,具体可查看手册)
alpha: 透明度 (默认是1.0,不透明,0为完全透明)
cmap:Matplotlib的颜色映射,默认None; 用来表示节点对应的强度
vmin,vmax:浮点数,默认None;节点颜色映射尺度的最大和最小值
linewidths:[None|标量|一列值];图像边界的线宽
width: 边的宽度 (默认为1.0)
edge_color: 边的颜色(默认为黑色)
edge_cmap:Matplotlib的颜色映射,默认None; 用来表示边对应的强度
edge_vmin,edge_vmax:浮点数,默认None;边的颜色映射尺度的最大和最小值
style: 边的样式(默认为实现,可选:solid|dashed|dotted,dashdot)
labels:字典元素,默认None;文本形式的节点标签
font_size: 节点标签字体大小 (默认为12)
font_color: 节点标签字体颜色(默认为黑色)
node_size:节点大小
font_weight:字符串,默认’normal’
font_family:字符串,默认’sans-serif’
布局指定节点排列形式
circular_layout:节点在一个圆环上均匀分布
random_layout:节点随机分布shell_layout:节点在同心圆上分布
spring_layout:用Fruchterman-Reingold算法排列节点,中心放射状分布
spectral_layout:根据图的拉普拉斯特征向量排列节点
布局也可用pos参数指定,例如,nx.draw(G, pos = spring_layout(G)) 这样指定了networkx上以中心放射状分布.
2 Graph-无向图
如果添加的节点和边是已经存在的,是不会报错的,NetworkX会自动忽略掉已经存在的边和节点的添加。
节点
常用函数
nodes(G):在图节点上返回一个迭代器
number_of_nodes(G):返回图中节点的数量
all_neighbors(graph, node):返回图中节点的所有邻居
non_neighbors(graph, node):返回图中没有邻居的节点
common_neighbors(G, u, v):返回图中两个节点的公共邻居
1import networkx as nx
2import matplotlib.pyplot as plt
3G = nx.Graph()
4
5G.add_node('a')
6G.add_nodes_from(['b', 'c', 'd', 'e'])
7G.add_cycle(['f', 'g', 'h', 'j'])
8H = nx.path_graph(10)
9G.add_nodes_from(H)
10G.add_node(H)
11
12nx.draw(G, with_labels=True,node_color='red')
13plt.show()
14
15
16print('图中所有的节点', G.nodes())
17
18
19print('图中节点的个数', G.number_of_nodes())
20
21
22
23G.remove_node(1)
24G.remove_nodes_from(['b','c','d','e'])
边
常用函数
edges(G[, nbunch]):返回与nbunch中的节点相关的边的视图
number_of_edges(G):返回图中边的数目
non_edges(graph):返回图中不存在的边
1import networkx as nx
2import matplotlib.pyplot as plt
3
4
5
6F = nx.Graph()
7F.add_edge(11,12)
8
9
10e=(13,14)
11F.add_edge(*e)
12
13
14F.add_edges_from([(1,2),(1,3)])
15
16H = nx.path_graph(10)
17
18F.add_edges_from(H.edges())
19
20nx.draw(F, with_labels=True)
21plt.show()
22
23
24print('图中所有的边', F.edges())
25
26
27print('图中边的个数', F.number_of_edges())
28
29
30
31
32
33F.remove_edge(1,2)
34F.remove_edges_from([(11,12), (13,14)])
35
36nx.draw(F, with_labels=True)
37plt.show()
使用邻接迭代器遍历每一条边
1import networkx as nx
2import matplotlib.pyplot as plt
3
4
5FG = nx.Graph()
6FG.add_weighted_edges_from([(1,2,0.125), (1,3,0.75), (2,4,1.2), (3,4,0.275)])
7for n, nbrs in FG.adjacency_iter():
8 for nbr, eattr in nbrs.items():
9 data = eattr['weight']
10 print('(%d, %d, %0.3f)' % (n,nbr,data))
11
12
13
14
15
16
17
18
19
20print('***********************************')
21
22
23FG = nx.Graph()
24FG.add_weighted_edges_from([(1,2,0.125), (1,3,0.75), (2,4,1.2), (3,4,0.275)])
25for n, nbrs in FG.adjacency_iter():
26 for nbr, eattr in nbrs.items():
27 data = eattr['weight']
28 if data < 0.5:
29 print('(%d, %d, %0.3f)' % (n,nbr,data))
30
31
32
33
34
35print('***********************************')
36
37
38for u,v,d in FG.edges(data = 'weight'):
39 print((u,v,d))
40
41
42
43
属性
属性诸如weight,labels,colors,或者任何对象,都可以附加到图、节点或边上。
对于每一个图、节点和边都可以在关联的属性字典中保存一个(多个)键-值对。
默认情况下这些是一个空的字典,但是可以增加或者是改变这些属性。
图的属性
1
2
3import networkx as nx
4
5G = nx.Graph(day='Monday')
6print(G.graph)
7
8G.graph['day'] = 'Friday'
9print(G.graph)
10
11G.graph['name'] = 'time'
12print(G.graph)
13
14输出:
15{'day': 'Monday'}
16{'day': 'Friday'}
17{'day': 'Friday', 'name': 'time'}
节点的属性
1#节点的属性
2import networkx as nx
3
4G = nx.Graph(day='Monday')
5G.add_node(1, index='1th') #在添加节点时分配节点属性
6# print(G.node(data=True)) #TypeError: 'dict' object is not callable
7print(G.node)
8#{1: {'index': '1th'}}
9
10
11G.node[1]['index'] = '0th' #通过G.node[][]来添加或修改属性
12print(G.node)
13# {1: {'index': '0th'}}
14
15
16G.add_nodes_from([2,3], index='2/3th') #从集合中添加节点时分配属性
17print(G.node)
18# {1: {'index': '0th'}, 2: {'index': '2/3th'}, 3: {'index': '2/3th'}}
边的属性
1#边的属性
2import networkx as nx
3
4G = nx.Graph(day='manday')
5G.add_edge(1,2,weight=10) #在添加边时分配属性
6print(G.edges(data=True))
7#[(1, 2, {'weight': 10})]
8
9G.add_edges_from([(1,3), (4,5)], len=22) #从集合中添加边时分配属性
10print(G.edges(data='len'))
11# [(1, 2, None), (1, 3, 22), (4, 5, 22)]
12
13G.add_edges_from([(3,4,{'hight':10}),(1,4,{'high':'unknow'})])
14print(G.edges(data=True))
15# [(1, 2, {'weight': 10}), (1, 3, {'len': 22}), (1, 4, {'high': 'unknow'}), (3, 4, {'hight': 10}), (4, 5, {'len': 22})]
16
17
18G[1][2]['weight'] = 100000 #通过G[][][]来添加或修改属性
19print(G.edges(data=True))
20# [(1, 2, {'weight': 100000}), (1, 3, {'len': 22}), (1, 4, {'high': 'unknow'}), (3, 4, {'hight': 10}), (4, 5, {'len': 22})]
有向图和无向图互转
有向图和多重图的基本操作与无向图一致。
无向图与有向图之间可以相互转换,转化方法如下:
1
2
3H=DG.to_undirected()
4
5H=nx.Graph(DG)
6
7
8
9F = H.to_directed()
10
11F = nx.DiGraph(H)
3、DiGraph-有向图
1import networkx as nx
2import matplotlib.pyplot as plt
3
4G = nx.DiGraph()
5G.add_node(1)
6G.add_node(2)
7G.add_nodes_from([3,4,5,6])
8G.add_cycle([1,2,3,4])
9G.add_edge(1,3)
10G.add_edges_from([(3,5),(3,6),(6,7)])
11nx.draw(G,node_color = 'red')
12plt.savefig("youxiangtu.png")
13plt.show()
1import math
3
4import matplotlib.pyplot as plt
5import networkx as nx
6
7try:
8 import pygraphviz
9 from networkx.drawing.nx_agraph import graphviz_layout
10 layout = graphviz_layout
11except ImportError:
12 try:
13 import pydot
14 from networkx.drawing.nx_pydot import graphviz_layout
15 layout = graphviz_layout
16 except ImportError:
17 print("PyGraphviz and pydot not found;\n"
18 "drawing with spring layout;\n"
19 "will be slow.")
20 layout = nx.spring_layout
21
22n = 150
23
24p_giant = 1.0 / (n - 1)
25
26p_conn = math.log(n) / float(n)
27
28
29pvals = [0.003, 0.006, 0.008, 0.015]
30
31region = 220
32plt.subplots_adjust(left=0, right=1, bottom=0, top=0.95, wspace=0.01, hspace=0.01)
33for p in pvals:
34 G = nx.binomial_graph(n, p)
35 pos = layout(G)
36 region += 1
37 plt.subplot(region)
38 plt.title("p = %6.3f" % (p))
39 nx.draw(G, pos,
40 with_labels=False,
41 node_size=10
42 )
43
44 Gcc = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)
45 G0 = Gcc[0]
46 nx.draw_networkx_edges(G0, pos,
47 with_labels=False,
48 edge_color='r',
49 width=6.0
50 )
51
52 for Gi in Gcc[1:]:
53 if len(Gi) > 1:
54 nx.draw_networkx_edges(Gi, pos,
55 with_labels=False,
56 edge_color='r',
57 alpha=0.3,
58 width=5.0
59 )
60plt.show()
Random Geometric Graph 随机几何图
1import matplotlib.pyplot as plt
2import networkx as nx
3
4G = nx.random_geometric_graph(200, 0.125)
5
6pos = nx.get_node_attributes(G, 'pos')
7
8
9dmin = 1
10ncenter = 0
11for n in pos:
12 x, y = pos[n]
13 d = (x - 0.5)**2 + (y - 0.5)**2
14 if d < dmin:
15 ncenter = n
16 dmin = d
17
18
19p = dict(nx.single_source_shortest_path_length(G, ncenter))
20
21plt.figure(figsize=(8, 8))
22nx.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4)
23nx.draw_networkx_nodes(G, pos, nodelist=list(p.keys()),
24 node_size=80,
25 node_color=list(p.values()),
26 cmap=plt.cm.Reds_r)
27
28plt.xlim(-0.05, 1.05)
29plt.ylim(-0.05, 1.05)
30
31plt.show()
节点颜色渐变
1import networkx as nx
2import matplotlib.pyplot as plt
3G = nx.cycle_graph(24)
4pos = nx.spring_layout(G, iterations=200)
5nx.draw(G, pos, node_color=range(24), node_size=800, cmap=plt.cm.Blues)
6plt.savefig("node.jpg")
7plt.show()
边的颜色渐变
1import matplotlib.pyplot as plt
2import networkx as nx
3
4G = nx.star_graph(20)
5pos = nx.spring_layout(G)
6colors = range(20)
7nx.draw(G, pos, node_color='
8 width=4, edge_cmap=plt.cm.Blues, with_labels=False)
9plt.show()
Atlas
1import random
2
3try:
4 import pygraphviz
5 from networkx.drawing.nx_agraph import graphviz_layout
6except ImportError:
7 try:
8 import pydot
9 from networkx.drawing.nx_pydot import graphviz_layout
10 except ImportError:
11 raise ImportError("This example needs Graphviz and either "
12 "PyGraphviz or pydot.")
13
14import matplotlib.pyplot as plt
15
16import networkx as nx
17from networkx.algorithms.isomorphism.isomorph import graph_could_be_isomorphic as isomorphic
18from networkx.generators.atlas import graph_atlas_g
19
20
21def atlas6():
22 """ Return the atlas of all connected graphs of 6 nodes or less.
23 Attempt to check for isomorphisms and remove.
24 """
25
26 Atlas = graph_atlas_g()[0:208]
27
28 U = nx.Graph()
29 for G in Atlas:
30 zerodegree = [n for n in G if G.degree(n) == 0]
31 for n in zerodegree:
32 G.remove_node(n)
33 U = nx.disjoint_union(U, G)
34
35
36 C = (U.subgraph(c) for c in nx.connected_components(U))
37
38 UU = nx.Graph()
39
40 nlist = []
41 for G in C:
42
43 if not iso(G, nlist):
44 nlist.append(G)
45 UU = nx.disjoint_union(UU, G)
46 return UU
47
48
49def iso(G1, glist):
50 """Quick and dirty nonisomorphism checker used to check isomorphisms."""
51 for G2 in glist:
52 if isomorphic(G1, G2):
53 return True
54 return False
55
56
57if __name__ == '__main__':
58 G = atlas6()
59
60 print("graph has %d nodes with %d edges"
61 % (nx.number_of_nodes(G), nx.number_of_edges(G)))
62 print(nx.number_connected_components(G), "connected components")
63
64 plt.figure(1, figsize=(8, 8))
65
66 pos = graphviz_layout(G, prog="neato")
67
68 C = (G.subgraph(c) for c in nx.connected_components(G))
69 for g in C:
70 c = [random.random()] * nx.number_of_nodes(g)
71 nx.draw(g,
72 pos,
73 node_size=40,
74 node_color=c,
75 vmin=0.0,
76 vmax=1.0,
77 with_labels=False
78 )
79 plt.show()
画个五角星
1import networkx as nx
2import matplotlib.pyplot as plt
3
4G=nx.Graph()
5G.add_node(1)
6G.add_nodes_from([2,3,4,5])
7for i in range(5):
8 for j in range(i):
9 if (abs(i-j) not in (1,4)):
10 G.add_edge(i+1, j+1)
11nx.draw(G,
12 with_labels=True,
13 edge_color='b',
14 pos=nx.circular_layout(G),
15
16
17 node_color='r',
18 node_size=1000,
19 width=3,
20 )
21plt.savefig("star.jpg")
22plt.show()
Club
1import matplotlib.pyplot as plt
2import networkx as nx
3import networkx.algorithms.bipartite as bipartite
4
5G = nx.davis_southern_women_graph()
6women = G.graph['top']
7clubs = G.graph['bottom']
8
9print("Biadjacency matrix")
10print(bipartite.biadjacency_matrix(G, women, clubs))
11
12
13W = bipartite.projected_graph(G, women)
14print('')
15print("#Friends, Member")
16for w in women:
17 print('%d %s' % (W.degree(w), w))
18
19
20
21W = bipartite.weighted_projected_graph(G, women)
22print('')
23print("#Friend meetings, Member")
24for w in women:
25 print('%d %s' % (W.degree(w, weight='weight'), w))
26
27nx.draw(G,node_color="red")
28plt.savefig("club.jpg")
29plt.show()
画一个多层感知机
1import matplotlib.pyplot as plt
2import networkx as nx
3left, right, bottom, top, layer_sizes = .1, .9, .1, .9, [4, 7, 7, 2]
4# 网络离上下左右的距离
5# layter_sizes可以自己调整
6import random
7G = nx.Graph()
8v_spacing = (top - bottom)/float(max(layer_sizes))
9h_spacing = (right - left)/float(len(layer_sizes) - 1)
10node_count = 0
11for i, v in enumerate(layer_sizes):
12 layer_top = v_spacing*(v-1)/2. + (top + bottom)/2.
13 for j in range(v):
14 G.add_node(node_count, pos=(left + i*h_spacing, layer_top - j*v_spacing))
15 node_count += 1
16# 这上面的数字调整我想了好半天,汗
17for x, (left_nodes, right_nodes) in enumerate(zip(layer_sizes[:-1], layer_sizes[1:])):
18 for i in range(left_nodes):
19 for j in range(right_nodes):
20 G.add_edge(i+sum(layer_sizes[:x]), j+sum(layer_sizes[:x+1]))
21
22pos=nx.get_node_attributes(G,'pos')
23# 把每个节点中的位置pos信息导出来
24nx.draw(G, pos,
25 node_color=range(node_count),
26 with_labels=True,
27 node_size=200,
28 edge_color=[random.random() for i in range(len(G.edges))],
29 width=3,
30 cmap=plt.cm.Dark2, # matplotlib的调色板,可以搜搜,很多颜色
31 edge_cmap=plt.cm.Blues
32 )
33plt.savefig("mlp.jpg")
34plt.show()
绘制一个DNN结构图
1
2import networkx as nx
3import matplotlib.pyplot as plt
4
5
6G = nx.DiGraph()
7
8
9vertex_list = ['v'+str(i) for i in range(1, 22)]
10
11G.add_nodes_from(vertex_list)
12
13
14edge_list = [
15 ('v1', 'v5'), ('v1', 'v6'), ('v1', 'v7'),('v1', 'v8'),('v1', 'v9'),
16 ('v2', 'v5'), ('v2', 'v6'), ('v2', 'v7'),('v2', 'v8'),('v2', 'v9'),
17 ('v3', 'v5'), ('v3', 'v6'), ('v3', 'v7'),('v3', 'v8'),('v3', 'v9'),
18 ('v4', 'v5'), ('v4', 'v6'), ('v4', 'v7'),('v4', 'v8'),('v4', 'v9'),
19 ('v5','v10'),('v5','v11'),('v5','v12'),('v5','v13'),('v5','v14'),('v5','v15'),
20 ('v6','v10'),('v6','v11'),('v6','v12'),('v6','v13'),('v6','v14'),('v6','v15'),
21 ('v7','v10'),('v7','v11'),('v7','v12'),('v7','v13'),('v7','v14'),('v7','v15'),
22 ('v8','v10'),('v8','v11'),('v8','v12'),('v8','v13'),('v8','v14'),('v8','v15'),
23 ('v9','v10'),('v9','v11'),('v9','v12'),('v9','v13'),('v9','v14'),('v9','v15'),
24 ('v10','v16'),('v10','v17'),('v10','v18'),
25 ('v11','v16'),('v11','v17'),('v11','v18'),
26 ('v12','v16'),('v12','v17'),('v12','v18'),
27 ('v13','v16'),('v13','v17'),('v13','v18'),
28 ('v14','v16'),('v14','v17'),('v14','v18'),
29 ('v15','v16'),('v15','v17'),('v15','v18'),
30 ('v16','v19'),
31 ('v17','v20'),
32 ('v18','v21')
33 ]
34
35G.add_edges_from(edge_list)
36
37
38plt.title('DNN for iris')
39
40nx.draw(
41 G,
42 node_color = 'red',
43 edge_color = 'black',
44 with_labels = True,
45 font_size =10,
46 node_size =300
47 )
48
49plt.show()
可以看到,在代码中已经设置好了这22个神经元以及它们之间的连接情况,但绘制出来的结构如却是这样的:
这显然不是想要的结果,因为各神经的连接情况不明朗,而且很多神经都挤在了一起,看不清楚。之所以出现这种情况,是因为没有给神经元设置坐标,导致每个神经元都是随机放置的。
接下来,引入坐标机制,即设置好每个神经元节点的坐标,使得它们的位置能够按照事先设置好的来放置,其Python代码如下:
1
2import networkx as nx
3import matplotlib.pyplot as plt
4
5
6G = nx.DiGraph()
7
8
9vertex_list = ['v'+str(i) for i in range(1, 22)]
10
11G.add_nodes_from(vertex_list)
12
13
14edge_list = [
15 ('v1', 'v5'), ('v1', 'v6'), ('v1', 'v7'),('v1', 'v8'),('v1', 'v9'),
16 ('v2', 'v5'), ('v2', 'v6'), ('v2', 'v7'),('v2', 'v8'),('v2', 'v9'),
17 ('v3', 'v5'), ('v3', 'v6'), ('v3', 'v7'),('v3', 'v8'),('v3', 'v9'),
18 ('v4', 'v5'), ('v4', 'v6'), ('v4', 'v7'),('v4', 'v8'),('v4', 'v9'),
19 ('v5','v10'),('v5','v11'),('v5','v12'),('v5','v13'),('v5','v14'),('v5','v15'),
20 ('v6','v10'),('v6','v11'),('v6','v12'),('v6','v13'),('v6','v14'),('v6','v15'),
21 ('v7','v10'),('v7','v11'),('v7','v12'),('v7','v13'),('v7','v14'),('v7','v15'),
22 ('v8','v10'),('v8','v11'),('v8','v12'),('v8','v13'),('v8','v14'),('v8','v15'),
23 ('v9','v10'),('v9','v11'),('v9','v12'),('v9','v13'),('v9','v14'),('v9','v15'),
24 ('v10','v16'),('v10','v17'),('v10','v18'),
25 ('v11','v16'),('v11','v17'),('v11','v18'),
26 ('v12','v16'),('v12','v17'),('v12','v18'),
27 ('v13','v16'),('v13','v17'),('v13','v18'),
28 ('v14','v16'),('v14','v17'),('v14','v18'),
29 ('v15','v16'),('v15','v17'),('v15','v18'),
30 ('v16','v19'),
31 ('v17','v20'),
32 ('v18','v21')
33 ]
34
35G.add_edges_from(edge_list)
36
37
38pos = {
39 'v1':(-2,1.5),
40 'v2':(-2,0.5),
41 'v3':(-2,-0.5),
42 'v4':(-2,-1.5),
43 'v5':(-1,2),
44 'v6': (-1,1),
45 'v7':(-1,0),
46 'v8':(-1,-1),
47 'v9':(-1,-2),
48 'v10':(0,2.5),
49 'v11':(0,1.5),
50 'v12':(0,0.5),
51 'v13':(0,-0.5),
52 'v14':(0,-1.5),
53 'v15':(0,-2.5),
54 'v16':(1,1),
55 'v17':(1,0),
56 'v18':(1,-1),
57 'v19':(2,1),
58 'v20':(2,0),
59 'v21':(2,-1)
60 }
61
62plt.title('DNN for iris')
63plt.xlim(-2.2, 2.2)
64plt.ylim(-3, 3)
65nx.draw(
66 G,
67 pos = pos,
68 node_color = 'red',
69 edge_color = 'black',
70 with_labels = True,
71 font_size =10,
72 node_size =300
73 )
74
75plt.show()
可以看到,在代码中,通过pos字典已经规定好了每个神经元节点的位置。
接下来,需要对这个框架图进行更为细致地修改,需要修改的地方为:
其中,第二步的文字注释,我们借助opencv来完成。完整的Python代码如下:
1
2import cv2
3import networkx as nx
4import matplotlib.pyplot as plt
5
6
7G = nx.DiGraph()
8
9
10vertex_list = ['v'+str(i) for i in range(1, 22)]
11
12G.add_nodes_from(vertex_list)
13
14
15edge_list = [
16 ('v1', 'v5'), ('v1', 'v6'), ('v1', 'v7'),('v1', 'v8'),('v1', 'v9'),
17 ('v2', 'v5'), ('v2', 'v6'), ('v2', 'v7'),('v2', 'v8'),('v2', 'v9'),
18 ('v3', 'v5'), ('v3', 'v6'), ('v3', 'v7'),('v3', 'v8'),('v3', 'v9'),
19 ('v4', 'v5'), ('v4', 'v6'), ('v4', 'v7'),('v4', 'v8'),('v4', 'v9'),
20 ('v5','v10'),('v5','v11'),('v5','v12'),('v5','v13'),('v5','v14'),('v5','v15'),
21 ('v6','v10'),('v6','v11'),('v6','v12'),('v6','v13'),('v6','v14'),('v6','v15'),
22 ('v7','v10'),('v7','v11'),('v7','v12'),('v7','v13'),('v7','v14'),('v7','v15'),
23 ('v8','v10'),('v8','v11'),('v8','v12'),('v8','v13'),('v8','v14'),('v8','v15'),
24 ('v9','v10'),('v9','v11'),('v9','v12'),('v9','v13'),('v9','v14'),('v9','v15'),
25 ('v10','v16'),('v10','v17'),('v10','v18'),
26 ('v11','v16'),('v11','v17'),('v11','v18'),
27 ('v12','v16'),('v12','v17'),('v12','v18'),
28 ('v13','v16'),('v13','v17'),('v13','v18'),
29 ('v14','v16'),('v14','v17'),('v14','v18'),
30 ('v15','v16'),('v15','v17'),('v15','v18'),
31 ('v16','v19'),
32 ('v17','v20'),
33 ('v18','v21')
34 ]
35
36G.add_edges_from(edge_list)
37
38
39pos = {
40 'v1':(-2,1.5),
41 'v2':(-2,0.5),
42 'v3':(-2,-0.5),
43 'v4':(-2,-1.5),
44 'v5':(-1,2),
45 'v6': (-1,1),
46 'v7':(-1,0),
47 'v8':(-1,-1),
48 'v9':(-1,-2),
49 'v10':(0,2.5),
50 'v11':(0,1.5),
51 'v12':(0,0.5),
52 'v13':(0,-0.5),
53 'v14':(0,-1.5),
54 'v15':(0,-2.5),
55 'v16':(1,1),
56 'v17':(1,0),
57 'v18':(1,-1),
58 'v19':(2,1),
59 'v20':(2,0),
60 'v21':(2,-1)
61 }
62
63plt.title('DNN for iris')
64plt.xlim(-2.2, 2.2)
65plt.ylim(-3, 3)
66nx.draw(
67 G,
68 pos = pos,
69 node_color = 'red',
70 edge_color = 'black',
71 font_size =10,
72 node_size =300
73 )
74
75
76plt.savefig('DNN_sketch.png')
77
78
79
80
81imagepath = 'DNN_sketch.png'
82image = cv2.imread(imagepath, 1)
83
84
85cv2.rectangle(image, (85, 130), (120, 360), (255,0,0), 2)
86cv2.putText(image, "Input Layer", (15, 390), 1, 1.5, (0, 255, 0), 2, 1)
87
88
89cv2.rectangle(image, (190, 70), (360, 420), (255,0,0), 2)
90cv2.putText(image, "Hidden Layer", (210, 450), 1, 1.5, (0, 255, 0), 2, 1)
91
92
93cv2.rectangle(image, (420, 150), (460, 330), (255,0,0), 2)
94cv2.putText(image, "Output Layer", (380, 360), 1, 1.5, (0, 255, 0), 2, 1)
95
96
97cv2.rectangle(image, (530, 150), (570, 330), (255,0,0), 2)
98cv2.putText(image, "Softmax Func", (450, 130), 1, 1.5, (0, 0, 255), 2, 1)
99
100
101cv2.imwrite('DNN.png', image)
一些图论算法
最短路径
函数调用:
1dijkstra_path(G, source, target, weight=‘weight’) ————求最短路径
2dijkstra_path_length(G, source, target, weight=‘weight’) ————求最短距离
3
4import networkx as nx
5import pylab
6import numpy as np
7
8row=np.array([0,0,0,1,2,3,6])
9col=np.array([1,2,3,4,5,6,7])
10value=np.array([1,2,1,8,1,3,5])
11
12print('生成一个空的有向图')
13G=nx.DiGraph()
14print('为这个网络添加节点...')
15for i in range(0,np.size(col)+1):
16 G.add_node(i)
17print('在网络中添加带权中的边...')
18for i in range(np.size(row)):
19 G.add_weighted_edges_from([(row[i],col[i],value[i])])
20
21print('给网路设置布局...')
22pos=nx.shell_layout(G)
23print('画出网络图像:')
24nx.draw(G,pos,with_labels=True, node_color='white', edge_color='red', node_size=400, alpha=0.5 )
25pylab.title('Self_Define Net',fontsize=15)
26pylab.show()
27
28
29'''
30Shortest Path with dijkstra_path
31'''
32print('dijkstra方法寻找最短路径:')
33path=nx.dijkstra_path(G, source=0, target=7)
34print('节点0到7的路径:', path)
35print('dijkstra方法寻找最短距离:')
36distance=nx.dijkstra_path_length(G, source=0, target=7)
37print('节点0到7的距离为:', distance)
输出:
1生成一个空的有向图
2为这个网络添加节点...
3在网络中添加带权中的边...
4给网路设置布局...
5画出网络图像:
6dijkstra方法寻找最短路径:
7节点0到7的路径: [0, 3, 6, 7]
8dijkstra方法寻找最短距离:
9节点0到7的距离为: 9
问题
本人在pycharm中运行下列程序:
1import networkx as nx
2import matplotlib.pyplot as plt
3
4G = nx.Graph()
5G.add_node('a')
6G.add_nodes_from(['b', 'c', 'd', 'e'])
7G.add_cycle(['f', 'g', 'h', 'j'])
8H = nx.path_graph(10)
9G.add_nodes_from(H)
10G.add_node(H)
11
12nx.draw(G, with_labels=True)
13plt.show()
发现在Pycharm下使用matploylib库绘制3D图的时候,在最后需要显示图像的时候,每当输入plt.show() 都会报错
1plt.show()
2/yyl/Python/3.6/lib/python/site-packages/matplotlib/figure.py:1743: UserWarning: This figure includes Axes that are not compatible with tight_layout, so its results might be incorrect.
3warnings.warn("This figure includes Axes that are not "
4...
5ValueError: max() arg is an empty sequence
网上的解决方案:File -> Setting -> Tools -> Python Scientific中去掉对Show plots in tool window的勾选就好了
一些其他神经网络绘制工具列表
上面都是一些这个网络库使用的一点总结,更多内容可以参考下面的官方链接。
参考
官方教程:https://networkx.github.io/documentation/stable/_downloads/networkx_reference.pdf
官方网站:https://networkx.github.io/documentation/latest/index.html
官方githu博客:http://networkx.github.io/
用Python的networkx绘制精美网络图:https://blog.csdn.net/qq951127336/article/details/54586869
networkx整理:https://www.cnblogs.com/minglex/p/9205160.html
Networkx使用指南:https://blog.csdn.net/Zhili_wang/article/details/89368177
论文中绘制神经网络工具汇总:https://blog.csdn.net/WZZ18191171661/article/details/87886588
networkx + Cytoscape构建及可视化网络图:https://www.jianshu.com/p/f62991aa1f8a
用python+graphviz/networkx画目录结构树状图:https://blog.csdn.net/XiaoPANGXia/article/details/53043664
扫码查看作者原文
▼▼▼
(*本文为 AI科技大本营转载文章,
转
载请
联系原作者
)
2019 中国大数据技术大会(BDTC)再度来袭!豪华主席阵容及百位技术专家齐聚,15 场精选专题技术和行业论坛,超强干货+技术剖析+行业实践立体解读,深入解析热门技术在行业中的实践落地。
即日起,
限量 5 折票
开售,数量有限,扫码购买,先到先得!
推荐阅读
巨头垂涎却不能染指,loT 数据库风口已至
【建议收藏】数据中心服务器基础知识大全
从4个维度深度剖析闪电网络现状,在CKB上实现闪电网络的理由 | 博文精选
身边程序员同事竟说自己敲代码速度快!Ctrl+C、Ctrl+V ?