開始使用免費開始

圖形清單

在這一組練習中,你會使用一個大學訊息傳遞資料集,學習如何為時間序列分析過濾圖形。在這個資料集中,節點是學生,邊表示從一位學生傳送訊息到另一位學生。現在的圖包含了所有時間點的所有通訊。

先從只會隨時間改變邊的圖開始分析吧。

這個資料集已載入為名為 data 的 DataFrame。你可以在 IPython Shell 中自由探索。特別是,先看看 data['sender']data['recipient'] 的輸出。

本練習屬於課程

Python 網路分析進階

檢視課程

練習說明

  • 初始化一個名為 Gs 的空 list。
  • 使用 for 迴圈走訪 months。在迴圈內:
    • 使用 nx.Graph() 建立一個新的無向圖,命名為 G
    • 將所有曾出現過的節點加入圖中。為此,在 G 上各呼叫一次 .add_nodes_from(),第一次以 data['sender'] 為引數,第二次以 data['recipient'] 為引數。
    • 將 DataFrame 過濾為僅包含指定月份。這一步已替你完成。
    • 從過濾後的 DataFrame 加入邊。為此,使用 .add_edges_from() 方法,並將 df_filtered['sender']df_filtered['recipient'] 傳入 zip()
    • G 加到圖形清單 Gs 中。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

import networkx as nx 

months = range(4, 11)

# Initialize an empty list: Gs
Gs = [] 
for month in months:
    # Instantiate a new undirected graph: G
    G = ____
    
    # Add in all nodes that have ever shown up to the graph
    ____
    ____
    
    # Filter the DataFrame so that there's only the given month
    df_filtered = data[data['month'] == month]
    
    # Add edges from filtered DataFrame
    ____
    
    # Append G to the list of graphs
    ____
    
print(len(Gs))
編輯並執行程式碼