开始使用免费开始使用

用户节点的度中心性分布

在本练习和下一个练习中,您将对上一个课程的内容做最后一次回顾。您的任务是针对 GitHub 协作网络的二部图版本,分别绘制各个节点分区的度中心性分布。本题针对 'users' 分区;下一题将针对 'projects' 分区。

您之前编写的函数 get_nodes_from_partition() 已为您加载。提醒一下,"度中心性"用于衡量节点的重要性,而"度中心性分布"是图中所有节点的度中心性得分列表。几道练习之前,您在绘制 circos 图时,我们已替您计算过度中心性。现在请您亲自实践计算!

本练习是课程的一部分

Python 网络分析中级

查看课程

练习说明

  • matplotlib.pyplotplt 的别名导入。
  • 使用上一题的 get_nodes_from_partition() 函数,获取与 G'users' 节点对应的列表,命名为 user_nodes
  • 使用 nx.degree_centrality() 计算 G 中每个节点的度中心性,并将结果存为 dcs
  • 使用列表推导式,计算 user_nodes 中每个节点的度中心性,并将结果存为 user_dcs
    • 请记住,dcs 是一个字典,键为节点。本题相关的节点包含在 user_nodes 中。如何利用这些信息获取用户节点的度中心性?请使用 n 作为迭代变量。
  • 使用 plt.hist()user_dcs 绘制用户的度分布直方图。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Import matplotlib
____

# Get the 'users' nodes: user_nodes
user_nodes = ____

# Compute the degree centralities: dcs
dcs = ____

# Get the degree centralities for user_nodes: user_dcs
user_dcs = [dcs[____] for n in ____]

# Plot the degree distribution of users_dcs
plt.yscale('log')
plt.hist(____, bins=20)
plt.show()
编辑并运行代码