開始使用免費開始

深度探討——Twitter 網路

接下來你要深入分析一個 Twitter 網路,藉此強化你先前學到的概念。首先,你會找出能夠在一度人脈範圍內,把訊息高效率傳播給大量使用者的節點。

NetworkX 已經替你預先匯入為 nx

本練習屬於課程

Python 網路分析入門

檢視課程

練習說明

  • 撰寫函式 find_nodes_with_highest_deg_cent(G),依下列步驟回傳具有最高度中心性的節點:
    • 計算 G 的度中心性。
    • list(deg_cent.values()) 使用 max() 來計算最大的度中心性。
    • 迭代度中心性字典 deg_cent.items()
    • 若當前節點 k 的度中心性值 v 等於 max_dc,就把它加入節點集合。
  • 使用你的函式找出在 T 中度中心性最高的節點。
  • 撰寫一個斷言敘述,檢查這些節點是否被正確識別。這部分已為你完成,按下「Submit Answer」即可查看結果!

動手互動練習

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

# Define find_nodes_with_highest_deg_cent()
def find_nodes_with_highest_deg_cent(G):

    # Compute the degree centrality of G: deg_cent
    deg_cent = ____

    # Compute the maximum degree centrality: max_dc
    max_dc = ____

    nodes = set()

    # Iterate over the degree centrality dictionary
    for k, v in ____:

        # Check if the current value has the maximum degree centrality
        if ____ == ____:

            # Add the current node to the set of nodes
            ____

    return nodes

# Find the node(s) that has the highest degree centrality in T: top_dc
top_dc = ____
print(top_dc)

# Write the assertion statement
for node in top_dc:
    assert nx.degree_centrality(T)[node] == max(nx.degree_centrality(T).values())
編輯並執行程式碼