深入解析-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中中介中心性最高的節點。 - 撰寫一個斷言來確認你得到正確的節點。這部分已為你完成,直接按下「Submit Answer」即可查看結果!
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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())