เจาะลึก - เครือข่าย Twitter
ในแบบฝึกหัดนี้จะเจาะลึกเครือข่าย Twitter เพื่อเสริมความเข้าใจในสิ่งที่ได้เรียนรู้ไปแล้ว โดยเริ่มจากการค้นหาโหนดที่สามารถกระจายข้อความได้อย่างมีประสิทธิภาพไปยังผู้คนจำนวนมากที่อยู่ห่างออกไปหนึ่งดีกรี
ได้นำเข้า NetworkX ไว้ให้แล้วในชื่อ nx
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์เครือข่ายเบื้องต้นด้วย Python
คำแนะนำการฝึกหัด
- เขียนฟังก์ชัน
find_nodes_with_highest_deg_cent(G)ที่คืนค่าโหนดซึ่งมี degree centrality สูงสุด โดยใช้ขั้นตอนต่อไปนี้:- คำนวณ degree centrality ของ
G - คำนวณค่า degree centrality สูงสุดโดยใช้ฟังก์ชัน
max()กับlist(deg_cent.values()) - วนซ้ำผ่าน dictionary ของ degree centrality คือ
deg_cent.items() - ถ้าค่า degree centrality
vของโหนดปัจจุบันkเท่ากับmax_dcให้เพิ่มโหนดนั้นลงในเซตของโหนด
- คำนวณ degree centrality ของ
- ใช้ฟังก์ชันที่สร้างขึ้นเพื่อค้นหาโหนดที่มี degree centrality สูงสุดใน
T - เขียน assertion statement เพื่อตรวจสอบว่าระบุโหนดได้ถูกต้อง ซึ่งได้เตรียมไว้ให้แล้ว กด ส่งคำตอบ เพื่อดูผลลัพธ์ได้เลย
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Define find_nodes_with_highest_deg_cent()
def find_nodes_with_highest_deg_cent(G):
# Compute the degree centrality of G: deg_cent
deg_cent = ____
# Compute the maximum degree centrality: max_dc
max_dc = ____
nodes = set()
# Iterate over the degree centrality dictionary
for k, v in ____:
# Check if the current value has the maximum degree centrality
if ____ == ____:
# Add the current node to the set of nodes
____
return nodes
# Find the node(s) that has the highest degree centrality in T: top_dc
top_dc = ____
print(top_dc)
# Write the assertion statement
for node in top_dc:
assert nx.degree_centrality(T)[node] == max(nx.degree_centrality(T).values())