绘制随时间变化的发帖数量
让我们回顾一下如何从图数据中绘制随时间演化的图统计量。首先,您将使用图数据来统计在一个长度为 td 天的时间窗口内出现的边的数量。在下面的练习中,td 为 2 天。
已为您提供日期时间变量 dayone 和 lastday。
本练习是课程的一部分
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()