射影グラフで次数中心性をプロットする
ここでは、次の各グラフについて次数中心性の分布を比較します。元のグラフ G、人物グラフの射影 peopleG、クラブグラフの射影 clubsG です。これにより、二部グラフと一部グラフでの次数中心性の計算方法の違いを確認できます。ノードリスト people と clubs はあらかじめ読み込まれています。
動画で説明したとおり、二部グラフ用の関数はノードのコンテナを引数として渡す必要がありますが、すべてのノードの次数中心性スコアを返します。また、次数中心性のスコアは辞書(ノードからスコアへの対応)として格納されることも思い出してください。
この演習はコースの一部です
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()