แสดงการกระจายของ degree centrality ใน students projection
ในแบบฝึกหัดนี้ จะได้แสดงการกระจายของ degree centrality ใน students projection ซึ่งเป็นการทบทวนแนวคิดสองเรื่องที่ได้เรียนไปแล้ว ได้แก่ degree centrality และ projection
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์เครือข่ายระดับกลางใน Python
คำแนะนำการฝึกหัด
- ดึงโหนดของ partition
'student'มาเก็บไว้ในลิสต์ชื่อstudent_nodes- ใช้ list comprehension โดยวนซ้ำผ่านโหนดทั้งหมดของ
G(รวมเมทาดาทา) แล้วตรวจสอบว่า key'bipartite'ของdมีค่าเท่ากับ'student'หรือไม่
- ใช้ list comprehension โดยวนซ้ำผ่านโหนดทั้งหมดของ
- สร้าง students nodes projection เป็นกราฟชื่อ
G_studentsโดยใช้ฟังก์ชันnx.bipartite.projected_graph()และระบุ keyword argumentnodes=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()