fail to run clustering with chinese_whispers : 'Graph' object has no attribute 'node'
yossibiton opened this issue · 4 comments
I'm using the following script to run different clustering methods over my data :
python tools/baseline_cluster.py ...
It works great with method knn_dbscan, but when i try chinese whispers i get the following error :
#nodes: 590, #edges: 50999
[Time] create graph consumes 0.8626 s
[Time] whisper iteratively (iters=20) consumes 0.0004 s
[Time] chinese_whispers consumes 1.0397 s
Traceback (most recent call last):
File "/Users/yossib/Dev/learn-to-cluster/tools/baseline_cluster.py", line 143, in <module>
pred_labels = cluster_func(feats, **args.__dict__)
File "/Users/yossib/Dev/learn-to-cluster/baseline/chinese_whispers.py", line 57, in chinese_whispers
assigned_cluster = G.node[nbr]['cluster']
AttributeError: 'Graph' object has no attribute 'node'
Process finished with exit code 1
@yossibiton Thanks for reporting. What is your python version? And do you mind changing this line to assigned_cluster = G[node][nbr]['cluster']
? As the graph is successfully built, the error may be raised by the way of fetching node
.
hello @yl-1993 , thanks for your response.
My python version is 3.6.9, and networkx package version is 2.4 - did you use other version ?
I already try your suggestion, and then i got another error on the same line :
File "/Users/yossib/Dev/learn_to_cluster/baseline/chinese_whispers.py", line 57, in chinese_whispers
assigned_cluster = G[node][nbr]['cluster']
KeyError: 'cluster'
I checked G[node][nbr] and it only has the 'weight' key.
So i guess something is broken with this script, and it's not just node fetching problem.
@yossibiton Thanks for the message. I am sorry that the new error is due to my previous suggestion. The reason lies in that G.node
is networkx.classes.reportviews.NodeView
while G[node]
is networkx.classes.coreviews.AtlasView
.
I figured out the problem is related to the version of networkx
, as reported in stackoverflow. There are two possible solutions:
- Downgrade your networkx via
pip install --upgrade networkx==2.3
. - Use the fixed version in PR #49
The second solution is suggested as it works fine under both 2.3 and 2.4 in my test.
Thank you very much, i took your PR and it works