AttributeError: 'Graph' object has no attribute 'node'

被刻印的时光 ゝ 提交于 2020-05-28 13:43:07

问题


I have bellow python code to build knn graph but I have an error: AttributeError: 'Graph' object has no attribute 'node'. It seems that the nx.Graph() has no node attribute but I don't know what should I replace with that.

import networkx as nx
def knn_graph(df, k, verbose=False):
    points = [p[1:] for p in df.itertuples()]
    g = nx.Graph()
    if verbose: print ("Building kNN graph (k = %d)" % (k))
    iterpoints = tqdm(enumerate(points), total=len(points)) if verbose else enumerate(points)
    for i, p in iterpoints:
        distances = map(lambda x: euclidean_distance(p, x), points)
        closests = np.argsort(distances)[1:k+1] # second trough kth closest
        for c in closests:
            g.add_edge(i, c, weight=distances[c])
        g.node[i]['pos'] = p
    return g

回答1:


If you are using NetworkX 2.4, use G.nodes[] instead of G.node[]. As the latter attribute is deprecated. See (https://networkx.github.io/documentation/stable/release/release_2.4.html#deprecations).



来源:https://stackoverflow.com/questions/58518554/attributeerror-graph-object-has-no-attribute-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!