描述編輯社群
現在你要把先前學到的 BFS 演算法與極大幫派(maximal clique)的概念結合起來,用 Arc plot 視覺化這個網路。
Github 使用者協作網路中最大的極大幫派已指定為子圖 G_lmc。請注意,在 NetworkX 2.x 版之後,G.subgraph(nodelist) 只會回傳原始圖的不可變檢視。我們必須明確呼叫 .copy() 才能取得可變動的版本。
本練習屬於課程
Python 網路分析入門
練習說明
- 從該幫派往外擴展 1 度關係,並把那些使用者加入子圖。在第一個
for迴圈中:- 使用
.add_nodes_from()與.neighbors()方法,從G的鄰居加入節點到G_lmc。 - 使用
.add_edges_from()方法,在目前節點與其所有鄰居之間加入邊。為此,你需要用zip()函式建立一個由配對所組成的清單,每一對包含目前節點與各個鄰居。zip()的第一個引數應為[node]*len(list(G.neighbors(node))),第二個引數應為node的所有鄰居。
- 使用
- 在每個節點的中繼資料中紀錄其度中心性分數。
- 在第二個
for迴圈中,將nx.degree_centrality(G_lmc)[n]指派給G_lmc.nodes[n]['degree centrality']。
- 在第二個
- 以 Arc plot 視覺化這個網路,並依度中心性排序節點(你可以使用關鍵字參數
sort_by='degree centrality'完成)。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import necessary modules
from nxviz import arc
import matplotlib.pyplot as plt
# Identify the largest maximal clique: largest_max_clique
largest_max_clique = set(sorted(nx.find_cliques(G), key=lambda x: len(x))[-1])
# Create a subgraph from the largest_max_clique: G_lmc
G_lmc = G.subgraph(largest_max_clique).copy()
# Go out 1 degree of separation
for node in list(G_lmc.nodes()):
G_lmc.add_nodes_from(____)
G_lmc.add_edges_from(zip(____, ____))
# Record each node's degree centrality score
for n in G_lmc.nodes():
____ = ____
# Create the Arc plot: a
a = ____
# Draw the Arc plot to the screen
a
plt.show()