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

เจาะลึก - เครือข่าย 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 สูงสุดใน 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())
แก้ไขและรันโค้ด