เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

คำนวณค่าเฉลี่ย Degree Centrality รายวันในกลุ่มนักเรียน

ในแบบฝึกหัดนี้ เราจะตรวจสอบว่าค่าเฉลี่ย degree centrality ของโหนดทั้งหมดมีความสัมพันธ์กับจำนวน edge ที่เปลี่ยนแปลงไปตามเวลาหรือไม่ ซึ่งอาจจะไม่มีความสัมพันธ์ที่ชัดเจนนัก และเราจะมาดูกันว่าเป็นเช่นนั้นจริงหรือเปล่า

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การวิเคราะห์เครือข่ายระดับกลางใน Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • สร้างกราฟใหม่ชื่อ G_sub ที่เก็บ edge เฉพาะส่วน
  • เพิ่มโหนดจาก G พร้อมเมทาดาทาของโหนด
  • เพิ่ม edge ที่ตรงตามเงื่อนไขโดยใช้เมธอด .add_edges_from()
  • ดึง students projection G_student_sub จาก G_sub โดยใช้ฟังก์ชัน nx.bipartite.projected_graph()
  • คำนวณ degree centrality ของ students projection โดยใช้ nx.degree_centrality() (อย่าใช้เวอร์ชัน bipartite)
  • เพิ่มค่าเฉลี่ย degree centrality ลงในลิสต์ mean_dcs โดยแปลง dc.values() เป็นลิสต์ก่อน
  • กด 'ส่งคำตอบ' เพื่อดูกราฟผลลัพธ์

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

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()
แก้ไขและรันโค้ด