开始使用免费开始使用

按天提取学生分区的平均度中心性

在这里,您将查看所有节点的平均度中心性是否与随时间绘制的边数相关。二者不一定存在强相关,您将检验是否如此。

本练习是课程的一部分

Python 网络分析中级

查看课程

练习说明

  • 实例化一个名为 G_sub 的新图,包含一部分边。
  • G 中添加节点,并包含节点元数据。
  • 使用 .add_edges_from() 方法添加满足条件的边。
  • 使用 nx.bipartite.projected_graph()G_sub 得到学生投影 G_student_sub
  • 使用 nx.degree_centrality() 计算学生投影的度中心性(不要使用二部图版本)。
  • 将平均度中心性追加到列表 mean_dcs。请先将 dc.values() 转为列表。
  • 点击 "Submit Answer" 查看绘图!

交互式实操练习

通过完成这段示例代码来试试这个练习。

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()
编辑并运行代码