開始使用免費開始

找出度中心性最高的節點

在這個練習中,你要更深入觀察網路中連結最多的學生是否有什麼有趣的特徵。首先,你會找出擁有最高度中心性的學生群組。這個結果會在下一題的繪圖練習中使用。

本練習屬於課程

Python 網路分析進階

檢視課程

練習說明

  • 取得前 5 個「不重複」的度中心性分數。做法是使用 sorted() 函式:第一個參數放 G 的度中心性值的「集合」(因為你需要不重複的度中心性),第二個參數設為 reverse=True,以確保以遞減順序排序。為了只保留前 5 個分數,請在敘述末端加入適當的切片。同時,記得對回傳的度中心性結果使用 .values()
  • 建立一個清單,包含整體度中心性排名前 5 的節點。做法如下:
    • nx.degree_centrality(G) 使用 .items(),以迭代度中心性分數的字典。
    • dctop_dcs 中,則將節點 n 加入 top_connected 清單。
  • 使用 len() 列印共有哪些節點共享前 5 個度中心性分數(top_connected)。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Get the top 5 unique degree centrality scores: top_dcs
top_dcs = ____(set(____), reverse=True)[____:____]

# Create list of nodes that have the top 5 highest overall degree centralities
top_connected = []
for n, dc in ____:
    if ____ in ____:
        ____
        
# Print the number of nodes that share the top 5 degree centrality scores
print(____)
編輯並執行程式碼