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

แสดงการกระจายของ degree centrality ใน students projection

ในแบบฝึกหัดนี้ จะได้แสดงการกระจายของ degree centrality ใน students projection ซึ่งเป็นการทบทวนแนวคิดสองเรื่องที่ได้เรียนไปแล้ว ได้แก่ degree centrality และ projection

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

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

ดูคอร์ส

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

  • ดึงโหนดของ partition 'student' มาเก็บไว้ในลิสต์ชื่อ student_nodes
    • ใช้ list comprehension โดยวนซ้ำผ่านโหนดทั้งหมดของ G (รวมเมทาดาทา) แล้วตรวจสอบว่า key 'bipartite' ของ d มีค่าเท่ากับ 'student' หรือไม่
  • สร้าง students nodes projection เป็นกราฟชื่อ G_students โดยใช้ฟังก์ชัน nx.bipartite.projected_graph() และระบุ keyword argument nodes=student_nodes ด้วย
  • คำนวณ degree centrality ของ G_students ด้วย nx.degree_centrality() แล้วเก็บผลลัพธ์ไว้ในตัวแปร dcs
  • พล็อตฮิสโทแกรมของค่า degree centrality

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

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

# Import necessary modules
import matplotlib.pyplot as plt
import networkx as nx

# Get the student partition's nodes: student_nodes
student_nodes = [n for n, d in ____ if d['____'] == '____']

# Create the students nodes projection as a graph: G_students
G_students = ____

# Calculate the degree centrality using nx.degree_centrality: dcs
dcs = ____

# Plot the histogram of degree centrality values
plt.hist(list(____))
plt.yscale('log')  
plt.show() 
แก้ไขและรันโค้ด