時間とともに投稿数をプロットする
グラフデータから変化するグラフ統計量をどのようにプロットするかをおさらいしましょう。まずは、td 日(下の演習では 2 日)という時間窓内に現れるエッジの数を、グラフデータを使って数え上げます。
日時変数 dayone と lastday はあらかじめ用意されています。
この演習はコースの一部です
Python 中級ネットワーク解析
演習の手順
timedelta()関数を使い、daysパラメータに引数を指定して 2 日のタイムデルタを定義します。whileループ内で行うこと:- スライディング時間窓内にあるようにエッジをフィルタしてください。これにはリスト内包表記を使います。出力式は
(u, v, d)、イテラブルはG.edges(data=True)、条件は 2 つで、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()