隨時間比較圖形差異
現在要計算圖形隨時間的差異!為了先看最簡單的情況,你會使用一個視窗(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(____)