시간에 따른 게시물 수 그리기
그래프 데이터에서 변화하는 그래프 통계를 어떻게 시각화하는지 복습해 보겠습니다. 먼저, 그래프 데이터를 사용해 td일(아래 연습에서는 2일)짜리 구간 시간 창 내에 나타나는 간선 수를 계산합니다.
dayone과 lastday라는 datetime 변수가 제공되어 있습니다.
이 연습은 강의의 일부입니다
Python 중급 네트워크 분석
연습 안내
timedelta()함수에서days매개변수에 인수를 지정해 2일짜리 timedelta를 정의하세요.while루프 내부에서:- 슬라이딩 시간 창에 속하는 간선만 필터링하세요. 리스트 내포를 사용하며, 출력 식은
(u, v, d), 반복 가능한 객체는G.edges(data=True)이고, 조건은 두 가지입니다:d['date']가>=curr_day이고curr_day+td보다<인 경우. - 간선의 개수(계산에는
len()함수를 사용하세요)를n_posts에 추가하세요. - 시간 델타
td만큼curr_day를 증가시키세요.
- 슬라이딩 시간 창에 속하는 간선만 필터링하세요. 리스트 내포를 사용하며, 출력 식은
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()