0

我正在尝试使用 NetworkX 库的 current_flow_betweenness_centrality 函数,但它在调用时给了我这个错误:

Traceback (most recent call last):   File "D:\IVV\pcg\procsator\__init__.py", line 47, in <module>
    el_val_rel = graph.elements_values(elements, conn_graph, m)   File "D:\IVV\pcg\procsator\graph.py", line 46, in elements_values
    conn_val = metric_func(g)   File "C:\Python27\lib\site-packages\networkx\algorithms\centrality\current_flow_betweenness.py", line 233, in current_flow_betweenness_centrality
    solver=solver):   File "C:\Python27\lib\site-packages\networkx\algorithms\centrality\flow_matrix.py", line 15, in flow_matrix_row
    dtype=dtype, format='csc')   File "C:\Python27\lib\site-packages\networkx\algorithms\centrality\flow_matrix.py", line 135, in laplacian_sparse_matrix
    dtype=dtype, format=format)   File "C:\Python27\lib\site-packages\networkx\convert.py", line 790, in to_scipy_sparse_matrix
    for u,v,d in G.edges_iter(nodelist, data=True) ValueError: need more than 0 values to unpack

这是我正在使用的代码:

import networkx as nx

g=nx.Graph()
a=object()
b=object()
c=object()
d=object()
g.add_edge(a,b,{'distance': 4.0})
g.add_edge(a,c,{'distance': 1.5})
g.add_edge(b,c,{'distance': 2.2})
g.add_edge(c,d,{'distance': 2.6})
result = nx.current_flow_betweenness_centrality(g, weight='distance')

我哪里错了?在 Windows 7 x64 上尝试使用 NetworkX 1.7 和 Python 2.7 和 3.3。

4

1 回答 1

1

更新:

这是https://github.com/networkx/networkx/pull/856中解决/修复的错误

原始(不正确)答案:

NetworkX 节点必须是 Python “可散列”对象。您的 object() 节点不是。(您收到的错误消息不是很有帮助)。

例如这有效

import networkx as nx

g=nx.Graph()
a=1
b=2
c=3
d=4
g.add_edge(a,b,{'distance': 4.0})
g.add_edge(a,c,{'distance': 1.5})
g.add_edge(b,c,{'distance': 2.2})
g.add_edge(c,d,{'distance': 2.6})
result = nx.current_flow_betweenness_centrality(g, weight='distance')
于 2013-03-16T20:17:54.227 回答