winform調用python,python networkx模塊_networkx模塊

 2023-11-12 阅读 25 评论 0

摘要:# # -*- coding: utf-8 -*-## #01-模塊導入# # import networkx as nx# # import matplotlib.pyplot as plt### # #02-無向圖graph# # G=nx.Graph()# # G.add_node(1)# # G.add_edge(2,3)# # G.add_edge(3,2)# # print "nodes:", G.nodes()# # print "edges:&

# # -*- coding: utf-8 -*-## #01-模塊導入# # import networkx as nx# # import matplotlib.pyplot as plt### # #02-無向圖graph# # G=nx.Graph()# # G.add_node(1)# # G.add_edge(2,3)# # G.add_edge(3,2)# # print "nodes:", G.nodes()# # print "edges:", G.edges()# # print "number of edges:", G.number_of_edges()# # nx.draw(G,pos=None,with_labels=True)# # plt.savefig('E:\\06-python\\lcj_networkx_exercise\\results\\wuxiangtu.png')# # plt.show()## #03-有向圖digraph# # G=nx.DiGraph()# # G.add_node(1)# # G.add_node(2)# # G.add_nodes_from([3,4,5,6])# # G.add_cycle([1,2,3,4])# # G.add_edge(1,3)# # G.add_edges_from([(3,5),(3,6),(6,7)])# # nx.draw(G,pos=None,with_labels=True)# # plt.savefig('E:\\06-python\\lcj_networkx_exercise\\results\\youxiangtu.png')# # plt.show()## #04-有向圖轉換為無向圖# # G1=G.to_undirected()# # plt.subplot(1,2,1)# # nx.draw(G,pos=None,with_labels=True)# # plt.subplot(1,2,2)# # nx.draw(G1,pos=None,with_labels=True)# # plt.show()## # G1=G.to_undirected()# # plt.figure(1)# # nx.draw(G,pos=None,with_labels=True)# # plt.figure(2)# # nx.draw(G1,pos=None,with_labels=True)# # plt.show()### # # #05-加權圖# # G=nx.Graph()# # G.add_edge(2,3)# # G.add_weighted_edges_from([(3,4,1.0),(3,5,10.5)])# # print G.get_edge_data(2,3)# # print G.get_edge_data(3,4)# # print G.get_edge_data(3,5)# # nx.draw(G,pos=None,with_labels=True)# # plt.savefig('E:\\06-python\\lcj_networkx_exercise\\results\\wuxiangjiaquan.png')# # plt.show()## # #06- 求無向圖的任意兩點間的最短路徑# # G = nx.Graph()# # G.add_edges_from([(1,2),(1,3),(1,4),(1,5),(4,5),(4,6),(5,6)])# # path = nx.all_pairs_shortest_path(G)# # print path[1]## #07-圖中指定兩點之間的最短路徑# # G=nx.Graph()# # G.add_nodes_from([1,2,3,4,5,6,7])# # G.add_edges_from([(1,2),(1,3),(2,3),(3,4),(3,6),(4,6),(2,5),(1,7)])# # nx.draw(G,pos=None,with_labels=True)# # plt.show()# # try:# # n=nx.shortest_path_length(G,1,4)# # print n# # except nx.NetworkXNoPath:# # print 'No path'## # #08-生成小世界網絡# # 用random_graphs.watts_strogatz_graph(n, k, p)# # 方法生成一個含有n個節點、每個節點有k個鄰居、以概率p隨機化重連邊的WS小世界網絡# # WS = nx.random_graphs.watts_strogatz_graph(20,4,0.3) #生成包含20個節點、每個節點4個近鄰、隨機化重連概率為0.3的小世界網絡# # pos = nx.circular_layout(WS) #定義一個布局,此處采用了circular布局方式# # nx.draw(WS,pos,with_labels=True,node_size = 30) #繪制圖形# # plt.savefig('E:\\06-python\\lcj_networkx_exercise\\results\\random_graphs.png')# # plt.show()## # # 09-生成ER隨即圖# # 模型的基本思想是以概率p連接N個節點中的每一對節點。# # 用random_graphs.erdos_renyi_graph(n,p)方法生成一個含有n個節點、以概率p連接的ER隨機圖# # ER = nx.random_graphs.erdos_renyi_graph(20,0.2) #生成包含20個節點、以概率0.2連接的隨機圖# # pos = nx.shell_layout(ER) #定義一個布局,此處采用了shell布局方式# # nx.draw(ER,pos,with_labels=True,node_size = 30)# # plt.savefig('E:\\06-python\\lcj_networkx_exercise\\results\\er_random_graphs.png')# # plt.show()## # 10-規則圖regular graph# # random_graphs.random_regular_graph(d, n)方法# # 可以生成一個含有n個節點,每個節點有d個鄰居節點的規則圖# # RG = nx.random_graphs.random_regular_graph(3,20) #生成包含20個節點、每個節點有3個鄰居的規則圖RG# # pos = nx.spectral_layout(RG) #定義一個布局,此處采用了spectral布局方式,后變還會介紹其它布局方式,注意圖形上的區別# # nx.draw(RG,pos,with_labels=True,node_size = 200) #繪制規則圖的圖形,with_labels決定節點是非帶標簽(編號),node_size是節點的直徑# # plt.savefig('E:\\06-python\\lcj_networkx_exercise\\results\\regular_graphs.png')# # plt.show() #顯示圖形## # #11-BA無標度網絡# # # 用random_graphs.barabasi_albert_graph(n, m)方法# # # 生成一個含有n個節點、每次加入m條邊的BA無標度網絡。# import networkx as nx# import matplotlib.pyplot as plt# BA=nx.random_graphs.barabasi_albert_graph(10,1)# nx.draw(BA,pos=None,with_labels=True)# plt.show()# # 12-用nx.path_graph()畫簡單網絡# # G=nx.path_graph(8)# # nx.draw(G,pos=None,with_labels=True)# # plt.show()## # # 13-Draw a graph with matplotlib, color by degree# # # G = nx.cycle_graph(24)# # G=nx.Graph()# # G.add_node(1)# # G.add_edge(2,3)# # G.add_edge(3,1)# # pos = nx.spring_layout(G, iterations=200)# # nx.draw(G, pos, node_color=range(3), node_size=800, cmap=plt.cm.Blues)# # plt.show()## # 14-Draw a graph with matplotlib, color edges# # import matplotlib.pyplot as plt# # import networkx as nx# ## # G = nx.star_graph(20)# # pos = nx.spring_layout(G)# # colors = range(20)# # nx.draw(G, pos, node_color='#A0CBE2', edge_color=colors,# # width=4, edge_cmap=plt.cm.Blues, with_labels=False)# # plt.show()## #15-網絡的整體特征# import networkx as nx# import matplotlib.pyplot as plt# G=nx.random_graphs.barabasi_albert_graph(10,3)#生成一個10個節點的BA無標度網絡# plt.figure(1)# nx.draw(G,pos=None,with_labels=True)# plt.show()# # 網絡平均聚集系數calculate average Clustering coefficient# print 'average clustering coefficient is ', nx.average_clustering(G)# # 每個節點的聚集系數# print "every node's clustering coefficient is ", nx.clustering(G)# # 網絡直徑calucate Diameter of G (the length of the longest shortest path)# print 'Diameter of G is ', nx.diameter(G)# # 網絡平均最短路徑calculate all nodes' average shortest path length# print "all nodes' average shortest path length is", nx.average_shortest_path_length(G)# 16-網絡中節點的度及度分布importnetworkx asnx

importmatplotlib.pyplot asplt

G=nx.random_graphs.barabasi_albert_graph(10,3)#生成一個10個節點的BA無標度網絡plt.figure(1)

nx.draw(G,pos=None,with_labels=True)

plt.show()

# print G.degree()#給出所有節點的度值# print G.degree(0)#給出node-0的度值# degree=nx.degree_histogram(G)# print nx.degree_histogram(G)#返回圖中所有節點的度分布序列(從1至最大度的出現頻次)# x=range(len(degree))# y=[z/float(sum(degree)) for z in degree]#度分布頻率# plt.figure(2)# plt.loglog(x,y,color='blue',linewidth=2)# plt.show()# #接近中心性# closeness_centrality=nx.closeness_centrality(G)# print closeness_centrality# #介數中心性# betweenness_centrality= nx.betweenness_centrality(G)# print betweenness_centrality#特征向量中心性eigenvector_centrality=nx.eigenvector_centrality(G)

printeigenvector_centrality

# # 17-有向網絡的度值# import networkx as nx# import matplotlib.pyplot as plt## G=nx.DiGraph()# G.add_node(1)# G.add_node(2)# G.add_nodes_from([3,4,5,6])# G.add_cycle([1,2,3,4])# G.add_edge(1,3)# G.add_edges_from([(3,5),(3,6),(6,7)])# nx.draw(G,pos=None,with_labels=True)# # plt.savefig('E:\\06-python\\lcj_networkx_exercise\\results\\youxiangtu.png')# plt.show()# print nx.degree_centrality(G) #Compute the degree centrality for nodes.# print nx.in_degree_centrality(G) # Compute the in-degree centrality for nodes.# print nx.out_degree_centrality(G) # Compute the out-degree centrality for nodes.

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/174482.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息