학생 파티션에서 날짜별 평균 차수 중심성 추출하기
이번에는 모든 노드의 평균 차수 중심성이 시간에 따라 그려진 엣지 수와 상관이 있는지 확인해 보겠습니다. 반드시 강한 상관이 있으리라는 보장은 없으니, 실제로 그런지 살펴보세요.
이 연습은 강의의 일부입니다
Python 중급 네트워크 분석
연습 안내
- 엣지의 부분집합을 포함하는 새 그래프
G_sub를 생성하세요. - 노드 메타데이터를 포함하여
G의 노드들을 추가하세요. - 조건을 충족하는 엣지들을
.add_edges_from()메서드로 추가하세요. nx.bipartite.projected_graph()함수를 사용해G_sub에서 학생 프로젝션G_student_sub를 구하세요.nx.degree_centrality()를 사용해 학생 프로젝션의 차수 중심성을 계산하세요(이분 그래프 전용 버전은 사용하지 마세요).- 평균 차수 중심성을 리스트
mean_dcs에 추가하세요. 이때dc.values()를 먼저 리스트로 변환해야 합니다. - 'Submit Answer'를 눌러 플롯을 확인하세요!
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
from datetime import datetime, timedelta
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
# Initialize a new list: mean_dcs
mean_dcs = []
curr_day = dayone
td = timedelta(days=2)
while curr_day < lastday:
if curr_day.day == 1:
print(curr_day)
# Instantiate a new graph containing a subset of edges: G_sub
G_sub = ____
# Add nodes from G
G_sub.____(____)
# Add in edges that fulfill the criteria
G_sub.____([(u, v, d) for u, v, d in G.edges(data=True) if d['date'] >= curr_day and d['date'] < curr_day + td])
# Get the students projection
G_student_sub = ____
# Compute the degree centrality of the students projection
dc = ____
# Append mean degree centrality to the list mean_dcs
mean_dcs.____(np.mean(____(dc.values())))
# Increment the time
curr_day += td
plt.plot(mean_dcs)
plt.xlabel('Time elapsed')
plt.ylabel('Degree centrality.')
plt.show()