深度解析——Twitter 网络(下)
接下来,您将以同样的方式深入分析介数中心性!给您几个提示:请记住,介数中心性可以使用 nx.betweenness_centrality(G) 计算。
本练习是课程的一部分
Python 网络分析入门
练习说明
- 编写函数
find_node_with_highest_bet_cent(G),返回介数中心性最高的节点(可为多个)。- 计算图
G的介数中心性。 - 使用
max()对list(bet_cent.values())求值,得到最大介数中心性。 - 遍历介数中心性字典
bet_cent.items()。 - 如果当前节点
k的介数中心性值v等于max_bc,则将其加入节点集合。
- 计算图
- 使用您的函数,找出在图
T中介数中心性最高的节点。 - 写一条断言语句以验证您找对了节点。本步骤已为您完成,点击 "提交答案" 查看结果!
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Define find_node_with_highest_bet_cent()
def find_node_with_highest_bet_cent(G):
# Compute betweenness centrality: bet_cent
bet_cent = ____
# Compute maximum betweenness centrality: max_bc
max_bc = ____
nodes = set()
# Iterate over the betweenness centrality dictionary
for k, v in ____:
# Check if the current value has the maximum betweenness centrality
if ____ == ____:
# Add the current node to the set of nodes
____
return nodes
# Use that function to find the node(s) that has the highest betweenness centrality in the network: top_bc
top_bc = ____
print(top_bc)
# Write an assertion statement that checks that the node(s) is/are correctly identified.
for node in top_bc:
assert nx.betweenness_centrality(T)[node] == max(nx.betweenness_centrality(T).values())