開始使用免費開始

逐日擷取學生分割上的平均度中心性

在這裡,你要檢查所有節點的平均度中心性,是否與隨時間繪製的邊數有相關。兩者不一定會有強相關,你將透過觀察來確認是否如此。

本練習屬於課程

Python 網路分析進階

檢視課程

練習說明

  • 建立一個名為 G_sub 的新圖,僅包含部分邊。
  • G 加入節點,包含節點的中介資料(metadata)。
  • 使用 .add_edges_from() 加入符合條件的邊。
  • 使用 nx.bipartite.projected_graph(),從 G_sub 取得學生投影 G_student_sub
  • 使用 nx.degree_centrality() 計算學生投影的度中心性(不要使用二分圖版本)。
  • 將平均度中心性加入清單 mean_dcs。請先把 dc.values() 轉成 list。
  • 按下「送出答案」來查看圖表!

動手互動練習

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

from datetime import datetime, timedelta
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt

# Initialize a new list: mean_dcs
mean_dcs = []
curr_day = dayone
td = timedelta(days=2)

while curr_day < lastday:
    if curr_day.day == 1:
        print(curr_day)  
    # Instantiate a new graph containing a subset of edges: G_sub
    G_sub = ____
    # Add nodes from G
    G_sub.____(____)
    # Add in edges that fulfill the criteria
    G_sub.____([(u, v, d) for u, v, d in G.edges(data=True) if d['date'] >= curr_day and d['date'] < curr_day + td])
    
    # Get the students projection
    G_student_sub = ____
    # Compute the degree centrality of the students projection
    dc = ____
    # Append mean degree centrality to the list mean_dcs
    mean_dcs.____(np.mean(____(dc.values())))
    # Increment the time
    curr_day += td
    
plt.plot(mean_dcs)
plt.xlabel('Time elapsed')
plt.ylabel('Degree centrality.')
plt.show()
編輯並執行程式碼