使用者節點的度中心性分佈
在這個練習與下一個練習中,你要回顧上一門課的重點。你的任務是為 GitHub 協作網路的二分圖版本中,每個節點分區繪製度中心性分佈。這裡先針對 'users' 分區進行;下一個練習則針對 'projects' 分區。
你先前寫好的函式 get_nodes_from_partition() 已為你載入。提醒一下,「度中心性」是衡量節點重要性的指標,而「度中心性分佈」是圖中所有節點的度中心性分數列表。幾個練習之前,在你製作 circos 圖時,我們已幫你計算過度中心性。現在輪到你自己動手練習!
本練習屬於課程
Python 網路分析進階
練習說明
- 將
matplotlib.pyplot以plt的別名匯入。 - 使用你在上一個練習的
get_nodes_from_partition()函式,取得對應G中'users'節點的清單,命名為user_nodes。 - 使用
nx.degree_centrality()計算G中每個節點的度中心性。將結果儲存為dcs。 - 使用串列生成式計算
user_nodes中每個節點的度中心性。將結果儲存為user_dcs。- 記住,
dcs是一個字典,鍵是節點。這裡相關的節點包含在user_nodes中。你要如何利用這項資訊取得使用者節點的度中心性?請使用n作為迭代變數。
- 記住,
- 使用
plt.hist()與user_dcs繪製使用者度分佈的長條圖。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import matplotlib
____
# Get the 'users' nodes: user_nodes
user_nodes = ____
# Compute the degree centralities: dcs
dcs = ____
# Get the degree centralities for user_nodes: user_dcs
user_dcs = [dcs[____] for n in ____]
# Plot the degree distribution of users_dcs
plt.yscale('log')
plt.hist(____, bins=20)
plt.show()