開始使用免費開始

隨時間比較圖形差異

現在要計算圖形隨時間的差異!為了先看最簡單的情況,你會使用一個視窗(month, month + 1),並持續紀錄隨時間新增或移除的邊。這個練習是為下一個練習做準備,下一步你會將這些變化視覺化。

本練習屬於課程

Python 網路分析進階

檢視課程

練習說明

  • for 迴圈裡:
    • Gs[i] 指派給 g1,將 Gs[i + window] 指派給 g2
    • 使用 nx.difference() 計算 g2g1 的差異。把結果加入 added
    • g1g2 的差異加入 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(____)
編輯並執行程式碼