可视化学生投影的度数中心性分布
在本练习中,您将可视化学生投影的度数中心性分布。这是对您之前学过的两个概念的回顾:度数中心性与投影。
本练习是课程的一部分
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()