開始使用免費開始

視覺化學生投影的度中心性分布

在這個練習中,你要視覺化學生投影的度中心性分布。這會複習你之前學過的兩個觀念:度中心性與投影。

本練習屬於課程

Python 網路分析進階

檢視課程

練習說明

  • 取得屬於 'student' 分區的節點,放入名為 student_nodes 的清單中。
    • 使用串列生成式來完成,遍歷 G 的所有節點(包含後設資料),並檢查 d'bipartite' 關鍵字是否等於 'student'
  • 建立學生節點的投影圖,命名為 G_students。請使用 nx.bipartite.projected_graph(),並指定關鍵字參數 nodes=student_nodes
  • 使用 nx.degree_centrality() 計算 G_students 的度中心性,將結果儲存為 dcs
  • 繪製度中心性值的直方圖。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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() 
編輯並執行程式碼