You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

python-igraph带cutoff的closeness centrality计算异常求助

Python-igraph带cutoff参数的节点接近中心性计算异常

我用python-igraph处理多个无向加权图,计算**边介数(edge betweenness)和节点接近中心性(node closeness)**时,发现边介数无论是否设置cutoff都能正常计算,但节点接近中心性只有在不设置cutoff时结果正确;设置cutoff后,输出结果仅为1或NaN,且该问题和图的规模、权重无关。

示例图与数据

示例图对应的边数据如下:

u_indexv_indexlength
011
021
032
043
121
342

创建图的代码:

import igraph as ig
import pandas as pd

edges = pd.DataFrame({
    "u_index": [0,0,0,0,1,3],
    "v_index": [1,2,3,4,2,4],
    "length": [1,1,2,3,1,2]
})

ntwrg = ig.Graph()
ntwrg.add_vertices(1+max(edges["u_index"].max(), edges["v_index"].max()))
ntwrg.add_edges(list(zip(edges["u_index"], edges["v_index"])))
ntwrg.es["length"] = edges["length"]

测试结果对比

未设置cutoff时的节点接近中心性(结果正常)

执行代码:

ntwrg.closeness(weights="length")

输出结果:

cls_cen
0.571429
0.444444
0.444444
0.400000
0.307692

设置cutoff=3时的节点接近中心性(预期vs实际)

执行代码:

ntwrg.closeness(weights="length", cutoff=3)

预期结果:

cls_cen
0.571429
0.600000
0.600000
0.400000
0.400000

实际返回结果:

Cls_cen
1.0
1.0
1.0
NaN
NaN

设置cutoff=3时的边介数(结果正常)

执行代码:

ntwrg.edge_betweenness(weights="length", cutoff=3)

输出结果:

Btw_cen
2.0
2.0
3.0
1.0
1.0
1.0

内容的提问来源于stack exchange,提问作者Taku Koh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 07:45:34