เจาะลึก - เครือข่าย Twitter ส่วนที่ 2
ต่อไปจะเจาะลึก betweenness centrality ในแบบเดียวกัน ข้อควรจำ: ใช้ nx.betweenness_centrality(G) ในการคำนวณ betweenness centrality
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์เครือข่ายเบื้องต้นด้วย Python
คำแนะนำการฝึกหัด
- เขียนฟังก์ชัน
find_node_with_highest_bet_cent(G)ที่คืนค่าโหนดซึ่งมี betweenness centrality สูงสุด- คำนวณ betweenness centrality ของ
G - คำนวณค่า betweenness centrality สูงสุดโดยใช้ฟังก์ชัน
max()กับlist(bet_cent.values()) - วนซ้ำผ่าน dictionary ของ degree centrality คือ
bet_cent.items() - ถ้าค่า degree centrality
vของโหนดปัจจุบันkเท่ากับmax_bcให้เพิ่มโหนดนั้นลงในเซต
- คำนวณ betweenness centrality ของ
- ใช้ฟังก์ชันที่เขียนขึ้นเพื่อค้นหาโหนดที่มี betweenness centrality สูงสุดใน
T - เขียน assertion statement เพื่อตรวจสอบความถูกต้อง ส่วนนี้เตรียมไว้ให้แล้ว กด ส่งคำตอบ เพื่อดูผลลัพธ์
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Define find_node_with_highest_bet_cent()
def find_node_with_highest_bet_cent(G):
# Compute betweenness centrality: bet_cent
bet_cent = ____
# Compute maximum betweenness centrality: max_bc
max_bc = ____
nodes = set()
# Iterate over the betweenness centrality dictionary
for k, v in ____:
# Check if the current value has the maximum betweenness centrality
if ____ == ____:
# Add the current node to the set of nodes
____
return nodes
# Use that function to find the node(s) that has the highest betweenness centrality in the network: top_bc
top_bc = ____
print(top_bc)
# Write an assertion statement that checks that the node(s) is/are correctly identified.
for node in top_bc:
assert nx.betweenness_centrality(T)[node] == max(nx.betweenness_centrality(T).values())