【複雜網路】複雜網路分析庫NetworkX學習筆記(4):統計指標計算
無論是實際網路還是對模型網路進行分析,都離不開對網路拓撲統計指標的計算。反映網路結構與動力學特性的統計指標有很多,Costa等的Characterization of Complex Networks: A Survey of measurements一文對此有全面的綜述,本文僅介紹一些常用的統計指標在NetworkX中如何計算。
一、度、度分佈
NetworkX可以用來統計圖中每個節點的度,並生成度分佈序列。下邊是一段示例程式碼(這段程式碼可以在Shell裡一行一行的輸入,也可以將其儲存為一個以py結尾的純文字檔案後直接執行),注意看註釋部分:
import networkx as nx
G = nx.random_graphs.barabasi_albert_graph(1000,3) #生成一個n=1000,m=3的BA無標度網路
print G.degree(0) #返回某個節點的度
print G.degree() #返回所有節點的度
print nx.degree_histogram(G) #返回圖中所有節點的度分佈序列(從1至最大度的出現頻次)
對上述結果稍作處理,就可以在Origin等軟體裡繪製度分佈曲線了,當然也可以用matplotlib直接作圖,在上述程式碼後接著輸入:
import matplotlib.pyplot as plt #匯入科學繪圖的matplotlib包
degree = nx.degree_histogram(G) #返回圖中所有節點的度分佈序列
x = range(len(degree)) #生成x軸序列,從1到最大度
y = [z / float(sum(degree)) for z in degree]
#將頻次轉換為頻率,這用到Python的一個小技巧:列表內涵,Python的確很方便:)
plt.loglog(x,y,color="blue",linewidth=2) #在雙對數座標軸上繪製度分佈曲線
plt.show() #顯示圖表
二、群聚係數
這個在NetworkX裡實現起來很簡單,只需要呼叫方法nx.average_clustering(G) 就可以完成平均群聚係數的計算,而呼叫nx.clustering(G) 則可以計算各個節點的群聚係數。
三、直徑和平均距離
nx.diameter(G)返回圖G的直徑(最長最短路徑的長度),而nx.average_shortest_path_length(G)則返回圖G所有節點間平均最短路徑長度。
四、匹配性
這個也比較簡單,呼叫 nx.degree_assortativity(G) 方法可以計算一個圖的度匹配性。
五、中心性
這個我大部分不知道怎麼翻譯,直接上NX的幫助文件吧,需要計算哪方面的centrality自己從裡邊找:)
Degree centrality measures.(點度中心性?)
degree_centrality(G) Compute the degree centrality for nodes.
in_degree_centrality(G) Compute the in-degree centrality for nodes.
out_degree_centrality(G) Compute the out-degree centrality for nodes.
Closeness centrality measures.(緊密中心性?)
closeness_centrality(G[, v, weighted_edges]) Compute closeness centrality for nodes.
Betweenness centrality measures.(介數中心性?)
betweenness_centrality(G[, normalized, ...]) Compute betweenness centrality for nodes.
edge_betweenness_centrality(G[, normalized, ...]) Compute betweenness centrality for edges.
Current-flow closeness centrality measures.(流緊密中心性?)
current_flow_closeness_centrality(G[, ...]) Compute current-flow closeness centrality for nodes.
Current-Flow Betweenness
Current-flow betweenness centrality measures.(流介數中心性?)
current_flow_betweenness_centrality(G[, ...]) Compute current-flow betweenness centrality for nodes.
edge_current_flow_betweenness_centrality(G) Compute current-flow betweenness centrality for edges.
Eigenvector centrality.(特徵向量中心性?)
eigenvector_centrality(G[, max_iter, tol, ...]) Compute the eigenvector centrality for the graph G.
eigenvector_centrality_numpy(G) Compute the eigenvector centrality for the graph G.
Load centrality.(徹底暈菜~~~)
load_centrality(G[, v, cutoff, normalized, ...]) Compute load centrality for nodes.
edge_load(G[, nodes, cutoff]) Compute edge load.
六、小結
上邊介紹的統計指標只是NetworkX能計算的指標中的一小部分內容,除此之外NetworkX還提供了很多(我還沒有用到過的)統計指標計算方法,感興趣的朋友可以去查NetworkX的線上幫助文件:http://networkx.lanl.gov/reference/index.html。對於加權圖的統計指標計算,NetworkX似乎沒有直接提供方法(也可能是我沒找到),估計需要自己動手編制一些程式來完成。
轉自:https://blog.csdn.net/u011367448/article/details/11481625