开始使用免费开始使用

可视化学生投影的度数中心性分布

在本练习中,您将可视化学生投影的度数中心性分布。这是对您之前学过的两个概念的回顾:度数中心性与投影。

本练习是课程的一部分

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() 
编辑并运行代码