始める無料で始める

時間とともに投稿数をプロットする

グラフデータから変化するグラフ統計量をどのようにプロットするかをおさらいしましょう。まずは、td 日(下の演習では 2 日)という時間窓内に現れるエッジの数を、グラフデータを使って数え上げます。

日時変数 dayonelastday はあらかじめ用意されています。

この演習はコースの一部です

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()  
コードを編集して実行