'numpy.int64'对象不可迭代

时间:2017-09-11 18:28:00

标签: python numpy networkx

for i in Train.index :


    preds = nx.jaccard_coefficient(G, ebunch = (Train['source_node'][i], Train['destination_node'][i]))
    for u,v,p in preds:
        print('(%d, %d) -> %.8f' % (u, v, p))
TypeError                                 Traceback (most recent call last)
<ipython-input-23-95e128c1b501> in <module>()
      3 
      4     preds = nx.jaccard_coefficient(G, ebunch = (Train['source_node'][i], Train['destination_node'][i]))
----> 5     for u,v,p in preds:
      6         print('(%d, %d) -> %.8f' % (u, v, p))

C:\ProgramData\Anaconda3\lib\site-packages\networkx\algorithms\link_prediction.py in <genexpr>(.0)
    136             return len(cnbors) / union_size
    137 
--> 138     return ((u, v, predict(u, v)) for u, v in ebunch)
    139 
    140 

TypeError: 'numpy.int64' object is not iterable

1 个答案:

答案 0 :(得分:0)

参数ebunch必须是可迭代的元组。您还没有显示足够的代码来了解Train['source_node'][i]Train['destination_node'][i]是什么,但根据错误消息,我怀疑它们是numpy.int64个对象。在这种情况下,您需要在ebunch参数中进一步嵌套这一个级别。例如,在这里我将ebunch设置为包含单个元组的列表:

preds = nx.jaccard_coefficient(G, ebunch = [(Train['source_node'][i], Train['destination_node'][i])])