Ana5.2 網絡節點的重要性分析
在此節中實作網絡節點重要性分析,即節點中心性 (centrality),其中所使用的重要性衡量方法包含:中介性 (betweenness)、群聚度 (clustering)、接近性 (closeness)、度 (degree)、k-核 (k-core)及PR值 (PageRank)等方法。
網絡節點的重要性分析之程式的概念流程如下:
# 顯示開始訊息
# 讀取網絡相關資訊的pickle檔
# 取得網絡G
# 計算網絡節點的中心性
# 儲存節點中心性結果到txt檔中
# 顯示結束訊息
- 參考檔案: network_centrality_analysis.py
# coding=utf-8
# 匯入模組
import copy
import forceatlas as fa
import matplotlib.pyplot as plt
import networkx as nx
import random
import sys
import pickle
from prettytable import PrettyTable
# 定義函數
def network_centrality_analysis():
print(">>> Network centrality analysis")
print()
# Load the Karate network pickle file
print("[Msg] Load network information of Karate network.")
net_info = dict()
with open('karate.pickle', 'rb') as f:
net_info = pickle.load(f)
# Calculate centrality of network nodes
print("[Msg] Calculate centrality of network nodes.")
G = net_info['graph']
betweeness = nx.betweenness_centrality(G)
clustering = nx.clustering(G)
closeness = nx. closeness_centrality(G)
degree = nx.degree(G)
k_core = nx.core_number(G)
page_rank = nx.pagerank(G)
# Save results to file
print("[Msg] Save centrality results to file.")
pt = PrettyTable()
pt.field_names = ["NodeID", "Betweenness", "Clustering", "Closeness", "Degree", "K-core", "PageRank"]
pt.align = "l"
pt.align["NodeID"] = "c"
pt.align["Degree"] = "c"
pt.align["K-core"] = "c"
for v in G:
pt.add_row([v, round(betweeness[v], 4), round(clustering[v], 4), round(closeness[v], 4), degree[v], k_core[v], round(page_rank[v], 4)])
with open("network-centrality-analysis-results.txt", "w") as f:
f.write(pt.get_string())
print()
print(">>> STOP Network centrality analysis")
if __name__ == "__main__":
network_centrality_analysis()
- 參考檔案:karate-centrality.txt
+--------+-------------+------------+-----------+--------+--------+----------+
| NodeID | Betweenness | Clustering | Closeness | Degree | K-core | PageRank |
+--------+-------------+------------+-----------+--------+--------+----------+
| 0 | 0.4376 | 0.15 | 0.569 | 16 | 4 | 0.097 |
| 1 | 0.0539 | 0.3333 | 0.4853 | 9 | 4 | 0.0529 |
| 2 | 0.1437 | 0.2444 | 0.5593 | 10 | 4 | 0.0571 |
| 3 | 0.0119 | 0.6667 | 0.4648 | 6 | 4 | 0.0359 |
| 4 | 0.0006 | 0.6667 | 0.3793 | 3 | 3 | 0.022 |
| 5 | 0.03 | 0.5 | 0.3837 | 4 | 3 | 0.0291 |
| 6 | 0.03 | 0.5 | 0.3837 | 4 | 3 | 0.0291 |
| 7 | 0.0 | 1.0 | 0.44 | 4 | 4 | 0.0245 |
| 8 | 0.0559 | 0.5 | 0.5156 | 5 | 4 | 0.0298 |
| 9 | 0.0008 | 0 | 0.4342 | 2 | 2 | 0.0143 |
| 10 | 0.0006 | 0.6667 | 0.3793 | 3 | 3 | 0.022 |
| 11 | 0.0 | 0 | 0.3667 | 1 | 1 | 0.0096 |
| 12 | 0.0 | 1.0 | 0.3708 | 2 | 2 | 0.0146 |
| 13 | 0.0459 | 0.6 | 0.5156 | 5 | 4 | 0.0295 |
| 14 | 0.0 | 1.0 | 0.3708 | 2 | 2 | 0.0145 |
| 15 | 0.0 | 1.0 | 0.3708 | 2 | 2 | 0.0145 |
| 16 | 0.0 | 1.0 | 0.2845 | 2 | 2 | 0.0168 |
| 17 | 0.0 | 1.0 | 0.375 | 2 | 2 | 0.0146 |
| 18 | 0.0 | 1.0 | 0.3708 | 2 | 2 | 0.0145 |
| 19 | 0.0325 | 0.3333 | 0.5 | 3 | 3 | 0.0196 |
| 20 | 0.0 | 1.0 | 0.3708 | 2 | 2 | 0.0145 |
| 21 | 0.0 | 1.0 | 0.375 | 2 | 2 | 0.0146 |
| 22 | 0.0 | 1.0 | 0.3708 | 2 | 2 | 0.0145 |
| 23 | 0.0176 | 0.4 | 0.3929 | 5 | 3 | 0.0315 |
| 24 | 0.0022 | 0.3333 | 0.375 | 3 | 3 | 0.0211 |
| 25 | 0.0038 | 0.3333 | 0.375 | 3 | 3 | 0.021 |
| 26 | 0.0 | 1.0 | 0.3626 | 2 | 2 | 0.015 |
| 27 | 0.0223 | 0.1667 | 0.4583 | 4 | 3 | 0.0256 |
| 28 | 0.0018 | 0.3333 | 0.4521 | 3 | 3 | 0.0196 |
| 29 | 0.0029 | 0.6667 | 0.3837 | 4 | 3 | 0.0263 |
| 30 | 0.0144 | 0.5 | 0.4583 | 4 | 4 | 0.0246 |
| 31 | 0.1383 | 0.2 | 0.541 | 6 | 3 | 0.0372 |
| 32 | 0.1452 | 0.197 | 0.5156 | 12 | 4 | 0.0717 |
| 33 | 0.3041 | 0.1103 | 0.55 | 17 | 4 | 0.1009 |
+--------+-------------+------------+-----------+--------+--------+----------+