時間に伴うグラフの差分
ここでは、時間に沿ったグラフの差分を計算します。最も単純なケースとして、(month, month + 1) のウィンドウを用い、時間の経過とともに増減したエッジを追跡します。この演習は、次の演習で時間変化を可視化するための準備です。
この演習はコースの一部です
Python 中級ネットワーク解析
演習の手順
forループの中で:Gs[i]をg1に、Gs[i + window]をg2に代入します。nx.difference()を使ってg2とg1の差分を計算し、その結果をaddedに追加します。g1とg2の差分をremovedに追加します。
fractional_changesを出力します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
import networkx as nx
# Instantiate a list of graphs that show edges added: added
added = []
# Instantiate a list of graphs that show edges removed: removed
removed = []
# Here's the fractional change over time
fractional_changes = []
window = 1
for i in range(len(Gs) - window):
g1 = Gs[____]
g2 = Gs[____ + ____]
# Compute graph difference here
added.append(____)
removed.append(____)
# Compute change in graph size over time
fractional_changes.append((len(g2.edges()) - len(g1.edges())) / len(g1.edges()))
# Print the fractional change
print(____)