Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Graph' object has no attribute 'nodes_iter' in networkx module python

I have the below function in python2.7 using networkx module which produces the error.

for H in networkx.connected_component_subgraphs(G):
    bestScore = -1.0
    for n, d in H.nodes_iter(data=True):
        if d['Score'] > bestScore:
            bestScore = d['Score']
            bestSV = n
    if bestSV is not None:
        selectedSVs.add(bestSV)

Error:

Traceback (most recent call last):
File "cnvClassifier.py", line 128, in <module>
for n, d in H.nodes_iter(data=True):
AttributeError: 'Graph' object has no attribute 'nodes_iter'

Does anybody have any idea what has been wrong?

like image 713
chas Avatar asked Nov 16 '15 11:11

chas


2 Answers

You are probably using the pre-release version of networkx-2.0 which has removed the nodes_iter() method and now provides the the nodes() method with the same functionality. See this for details on the networkx-2.0 changes.

like image 159
Aric Avatar answered Nov 05 '22 14:11

Aric


Just in case the link changes again, I'm going to post the actual solution here for future reference.

From NetworkX 2.0 forward, you should change the following line of code from:

for n, d in H.nodes_iter(data=True):

to:

for n, d in list(H.nodes(data=True)):
like image 9
SummerEla Avatar answered Nov 05 '22 13:11

SummerEla