始める無料で始める

射影グラフで次数中心性をプロットする

ここでは、次の各グラフについて次数中心性の分布を比較します。元のグラフ G、人物グラフの射影 peopleG、クラブグラフの射影 clubsG です。これにより、二部グラフと一部グラフでの次数中心性の計算方法の違いを確認できます。ノードリスト peopleclubs はあらかじめ読み込まれています。

動画で説明したとおり、二部グラフ用の関数はノードのコンテナを引数として渡す必要がありますが、すべてのノードの次数中心性スコアを返します。また、次数中心性のスコアは辞書(ノードからスコアへの対応)として格納されることも思い出してください。

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

Python 中級ネットワーク解析

コースを見る

演習の手順

  • 二部グラフモジュールの degree_centrality 関数(nx.bipartite.degree_centrality())を使って、元のグラフ G の次数中心性の分布をプロットします。引数は2つで、グラフ G とノードリストのいずれか(people または clubs)です。
  • NetworkX の通常(非二部グラフ)用の degree_centrality 関数(nx.degree_centrality())を使って、peopleG グラフの次数中心性の分布をプロットします。
  • NetworkX の通常(非二部グラフ)用の degree_centrality 関数(nx.degree_centrality())を使って、clubsG グラフの次数中心性の分布をプロットします。

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

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

import matplotlib.pyplot as plt 

# Plot the degree centrality distribution of both node partitions from the original graph
plt.figure()
original_dc = ____
# Remember that you can directly plot dictionary values.
plt.hist(____, alpha=0.5)
plt.yscale('log')
plt.title('Bipartite degree centrality')
plt.show()


# Plot the degree centrality distribution of the peopleG graph
plt.figure()  
people_dc = ____
plt.hist(____)
plt.yscale('log')
plt.title('Degree centrality of people partition')
plt.show()

# Plot the degree centrality distribution of the clubsG graph
plt.figure() 
clubs_dc = ____
plt.hist(____)
plt.yscale('log')
plt.title('Degree centrality of clubs partition')
plt.show()
コードを編集して実行