AttributeError: 'Graph' object has no attribute 'node'
Asked Answered
K

3

28

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
Kirbie answered 23/10, 2019 at 8:24 Comment(4)
I think it should be nodes.Pothunter
@Pothunter I'm sorry but it doesn't work. beacuse i have an error on it: line 178, in __getitem__return self._nodes[n]Kirbie
What is the error?Pothunter
@Kirbie can you please post the full tracebackTrin
B
55

If you are using NetworkX 2.4, use G.nodes[] instead of G.node[]. The latter attribute is deprecated. See the release notes.

Batfowl answered 28/10, 2019 at 20:34 Comment(1)
In my case, I installed NetworkX 2.0 to solve the error.Hymn
O
0

I had the same issue. I'm using Anaconda3. pip uninstall networkx did not work in Anaconda command window. I opened Anaconda powershell and did pip uninstall networkx and then came back to Anaconda command window to pip install networkx==2.3

After this the error is resolved

Ophthalmoscopy answered 25/4, 2021 at 6:42 Comment(0)
M
0

For me, this error was related to anaconda and miniconda. I have removed miniconda from WSL and no more errors:

remove miniconda
pip install networkx==2.3

then run the related code.

Morocco answered 15/2 at 17:3 Comment(2)
That solution seems specific to you. OP has not specified if working with Anaconda. Besides, if I read you correctly, you are suggesting to mix your conda-installed Python with pip, which is a dangerous path regarding the stability of your environment.Schizo
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewSchizo

© 2022 - 2024 — McMap. All rights reserved.