当我试图在一个123212节点和329512个边的网络上运行社区社区查找算法时,我一直得到上面的错误。
simpledatasetNX这里是一个NetworkX图形对象。下面是我最近的一篇文章:
greedy_modularity_communities(simpledatasetNX)
以及产出情况:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-6-a3b0c8705138> in <module>()
----> 1 greedy_modularity_communities(simpledatasetNX)
2 frames
/usr/local/lib/python3.7/dist-packages/networkx/algorithms/community/modularity_max.py in greedy_modularity_communities(G, weight, resolution)
98 if j != i
99 }
--> 100 for i in range(N)
101 }
102 dq_heap = [
/usr/local/lib/python3.7/dist-packages/networkx/algorithms/community/modularity_max.py in <dictcomp>(.0)
98 if j != i
99 }
--> 100 for i in range(N)
101 }
102 dq_heap = [
/usr/local/lib/python3.7/dist-packages/networkx/algorithms/community/modularity_max.py in <dictcomp>(.0)
96 - 2 * resolution * k[i] * k[j] * q0 * q0
97 for j in [node_for_label[u] for u in G.neighbors(label_for_node[i])]
---> 98 if j != i
99 }
100 for i in range(N)
AttributeError: 'NoneType' object has no attribute 'get'在多次尝试纠正这一错误后,我已经多次遇到这个精确的错误。这就是我开始的地方,以及我为解决这个问题所做的事情:
desired_graph_object = nx.Graph(multidigraphobject)
它似乎做了我想做的事情,在一个简单得多的测试图上,所以我做了这个,然后尝试运行算法,得到上面的错误。
我现在怀疑,我构造这两个图的方式有问题,因为无论我在简化的多向图对象上还是在图对象上运行算法,错误的性质都是相同的。
这是我构造图形对象的代码;构造多个有向图对象的代码本质上是复制粘贴的,并进行了一些小的调整,因此我不觉得有必要包含它。据我所知,这段代码构造的图形对象和前面讨论的多向图对象都看我打算如何实现它们。
%%time
for index, row in df.iterrows(): # *Go through our dataframe row by row*
if row["link_id"] != row["parent_id"]: # *Check that the current row is a response to someone else*
link_id_df = link_id_dataframe_dict[row["link_id"]] # *Get the desired thread's dataframe*
for index2, row2 in link_id_df.iterrows(): # *Iterate through the thread dataframe's rows (comments)*
if (row2["id"] in row["parent_id"]) and ( (row["author"],row2["author"]) not in nx.edges(G) ): # *Go until we find the comment whose id matches our original comment's parent_id, AND check that our current potential edge isn't already an edge*
G.add_edge(row["author"],row2["author"]) # *Add the desired edge.*
if row["subreddit"] == ("Daddit" or "daddit"): # *This line and the next three, add the necessary edge attributes.*
nx.set_edge_attributes(G,{(row["author"],row2["author"]): {"daddit": 1, "mommit": 0}})
else:
nx.set_edge_attributes(G,{(row["author"],row2["author"]): {"daddit": 0, "mommit": 1}})
elif (row2["id"] in row["parent_id"]) and ( (row["author"],row2["author"]) in nx.edges(G) ): # *If the edge already exists, ie these two users have interacted before, increase appropriate comment quantity*
if row["subreddit"] == ("Daddit" or "daddit"):
G[row["author"]][row2["author"]]["daddit"] += 1
else:
G[row["author"]][row2["author"]]["mommit"] += 1一些附加的上下文:我的原始数据集是一个巨大的数据框架,我想从它构建我的网络。每一行代表社交媒体网站上的评论或帖子。它涉及将注释的id链接到对第一个注释的回复的注释的父_id。link_id_dataframe_dict是一个字典,其中键是给定的线程,而与该键相关联的对象是该线程中所有注释的子数据格式(即与该link_id一起)。
其思想是,我们逐行遍历整个数据帧,标识此行/注释所包含的线程/ link_id,然后在关联的link_id数据框架中搜索我们正在考虑的行/注释是响应的另一个行/注释。当我们这样做时,我们在两个节点之间添加一个边,其中这个边代表评论,这两个节点是发布回复和被回复评论的用户。我们还通过添加带有该社区标记的属性1和其他社区的零作为跟踪这些用户交互位置的一种方式来记录这个评论回复所在的社区。对于这个版本的代码,如果这些用户以前交互过,我们也会注意到,通过在表示发生新交互的社区的属性中添加一个。
最新情况:
我从图中删除了自循环,但不幸的是仍然遇到相同的错误。
发布于 2021-09-04 02:00:12
看起来你遇到了一个已知的错误,这个错误已经被纠正了。更多详情如下:
https://github.com/networkx/networkx/pull/4996
我认为它还没有出现在最近发布的networkx版本中(看起来它将出现在2.7版中),但是如果您用下面的代码替换了所使用的算法:max.py,它应该会修复这个问题。
发布于 2021-10-20 09:18:41
将networkx从2.6.2更新到2.6.3为我解决了这个问题。
https://stackoverflow.com/questions/69006073
复制相似问题