始める無料で始める

Deep dive - Twitter network part II

次は、媒介中心性についても同様に深掘りしていきます。進めるうえでのヒントです: 媒介中心性は nx.betweenness_centrality(G) で計算することを思い出してください。

この演習はコースの一部です

Pythonで学ぶネットワーク分析入門

コースを見る

演習の手順

  • 最も媒介中心性が高いノードを返す関数 find_node_with_highest_bet_cent(G) を作成します。
    • G の媒介中心性を計算します。
    • list(bet_cent.values()) に対して max() 関数を使い、最大の媒介中心性を求めます。
    • 媒介中心性の辞書 bet_cent.items() を反復処理します。
    • 現在のノード k の媒介中心性の値 vmax_bc と等しければ、そのノードを集合に追加します。
  • 作成した関数を使って、T の中で最も媒介中心性が高いノードを見つけます。
  • ノードが正しいことを確認するためのアサーション文を書きます。これはすでに用意してあるので、"Submit Answer" を押して結果を確認してください!

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Define find_node_with_highest_bet_cent()
def find_node_with_highest_bet_cent(G):

    # Compute betweenness centrality: bet_cent
    bet_cent = ____

    # Compute maximum betweenness centrality: max_bc
    max_bc = ____

    nodes = set()

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

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

            # Add the current node to the set of nodes
            ____

    return nodes

# Use that function to find the node(s) that has the highest betweenness centrality in the network: top_bc
top_bc = ____
print(top_bc)

# Write an assertion statement that checks that the node(s) is/are correctly identified.
for node in top_bc:
    assert nx.betweenness_centrality(T)[node] == max(nx.betweenness_centrality(T).values())
コードを編集して実行