深度解析——Twitter 网络
接下来,您将对一个 Twitter 网络进行深入分析,以巩固之前所学的知识。首先,您将寻找那些能在一度关系范围内高效向大量用户广播消息的节点。
NetworkX 已为您预先导入为 nx。
本练习是课程的一部分
Python 网络分析入门
练习说明
- 编写函数
find_nodes_with_highest_deg_cent(G),按以下步骤返回度中心性最高的节点:- 计算
G的度中心性。 - 对
list(deg_cent.values())使用max()计算最大的度中心性值。 - 遍历度中心性字典
deg_cent.items()。 - 如果当前节点
k的度中心性值v等于max_dc,将其加入节点集合。
- 计算
- 使用您的函数,找到图
T中度中心性最高的节点。 - 编写断言语句,检查是否正确识别出该节点(或这些节点)。此步骤已为您完成,点击 "提交答案" 查看结果!
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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())