始める無料で始める

深掘り - Twitter ネットワーク

ここでは Twitter のネットワークを深掘りして、これまで学んだ内容を強化していきます。まずは、1 次の距離にいる多くのユーザーへ効率よくメッセージを広く届けられるノードを見つけます。

NetworkX はすでに nx としてインポートされています。

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

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

コースを見る

演習の手順

  • 次の手順で、最も高い次数中心性をもつノードを返す関数 find_nodes_with_highest_deg_cent(G) を作成します。
    • G の次数中心性を計算します。
    • list(deg_cent.values()) に対して max() を使い、最大の次数中心性を求めます。
    • 次数中心性の辞書 deg_cent.items() を反復処理します。
    • 現在のノード k の次数中心性の値 vmax_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())
コードを編集して実行