开始使用免费开始使用

绘制随时间变化的发帖数量

让我们回顾一下如何从图数据中绘制随时间演化的图统计量。首先,您将使用图数据来统计在一个长度为 td 天的时间窗口内出现的边的数量。在下面的练习中,td 为 2 天。

已为您提供日期时间变量 dayonelastday

本练习是课程的一部分

Python 网络分析中级

查看课程

练习说明

  • 使用 timedelta() 函数并为 days 参数指定值,定义一个 2 天的时间增量。
  • while 循环内:
    • 过滤处于滑动时间窗口内的边。请使用列表推导式实现,输出表达式为 (u, v, d),可迭代对象为 G.edges(data=True),并包含两个条件:d['date'] >= curr_day< curr_day + td
    • 将边的数量(使用 len() 函数计算)追加到 n_posts
    • curr_day 按时间增量 td 递增。
  • 使用 plt.plot() 绘制 n_posts

交互式实操练习

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

# Import necessary modules
from datetime import timedelta  
import matplotlib.pyplot as plt

# Define current day and timedelta of 2 days
curr_day = dayone
td = ____

# Initialize an empty list of posts by day
n_posts = []
while curr_day < lastday:
    if curr_day.day == 1:
        print(curr_day) 
    # Filter edges such that they are within the sliding time window: edges
    edges = [(____, ____, ____) for u, v, d in ____ if d['date'] >= ____ and d['date'] < ____ + ____]
    
    # Append number of edges to the n_posts list
    ____
    
    # Increment the curr_day by the time delta
    ____ += ____
    
# Create the plot
plt.plot(____)  
plt.xlabel('Days elapsed')
plt.ylabel('Number of posts')
plt.show()  
编辑并运行代码