AttributeError:“图形”对象没有属性“节点” [英] AttributeError: 'Graph' object has no attribute 'node'

查看:80
本文介绍了AttributeError:“图形”对象没有属性“节点”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的python代码来构建knn图,但出现错误:AttributeError: Graph对象没有属性 node。看来nx.Graph()没有节点属性,但是我不知道该用什么替换。

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


推荐答案

如果使用的是NetworkX 2.4,请使用G.nodes []代替G.node []。由于不推荐使用后者。请参阅( https://networkx.github.io/documentation/stable /release/release_2.4.html#deprecations )。

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).

这篇关于AttributeError:“图形”对象没有属性“节点”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆