繁体   English   中英

AttributeError: 'NoneType' 对象在使用 NetworkX 的 greedy_modularity_communities 时没有属性 'get'

[英]AttributeError: 'NoneType' object has no attribute 'get' when using greedy_modularity_communities from NetworkX

尝试在包含 123212 个节点和 329512 个边的网络上运行来自 NetworkX 的greedy_modularity_communities社区查找算法时,我不断收到上述错误。

这里的 simpledatasetNX 是一个 NetworkX Graph 对象。 这是我最近运行的:

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'

在多次尝试解决此问题后,我多次遇到此确切错误。 这是我开始的地方,以及我为修复它所做的事情:

  1. 首先,我从数据集构建了一个 NetworkX MultiDiGraph对象,我需要将其转换为Graph对象才能运行该算法。 我看着简单地做

desired_graph_object = nx.Graph(multidigraphobject)

它似乎在我制作的更简单的测试图中完成了我想要它做的事情,所以我这样做了,然后尝试运行算法,得到上述错误。

  1. 我对这个问题可能是什么感到困惑。 然后我想起我构建多向图对象的方式涉及边缘上的许多属性(这个原始对象上有接近 500,000 条边),所以当我试图将它简化为图形对象时,可能会发生一些奇怪的事情由于我没有做任何事情来说明这一点,因此将相同的两个节点之间的一条边合并在一起。 例如,这些属性中的一些是整数,一些是字符串。 这似乎是一个足够简单的解决方法:我所要做的就是从我的数据集中构建一个新的图形对象,并只保留一个属性用于与 greedy_modularity_communities 算法相关的分析。 所以我这样做了,最终得到了完全相同的错误。

我现在怀疑我构建这两个图的方式有问题,因为无论我在简化的多向图对象还是在图对象上运行算法,错误的性质都是相同的。

这是我构建图形对象的代码; 构造多向图对象的代码基本上是复制粘贴的,并进行了细微的调整以实现这一点,所以我觉得没有必要包含它。 据我所知,此代码构建的图形对象和前面讨论的多向图对象都符合我的预期。

%%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 链接到回复第一条评论的评论的 parent_id。 link_id_dataframe_dict 是一个字典,其中一个键是给定的线程,与该键关联的对象是该线程中所有评论的子数据帧(即,具有该 link_id)。

这个想法是我们逐行浏览整个数据帧,确定该行/评论所属的线程/link_id,然后我们在关联的link_id数据帧中搜索该行/评论所在的另一行/评论正在考虑是回应。 当我们这样做时,我们在两个节点之间添加一条边,这条边代表评论,这两个节点是发布回复的用户和被回复的评论。 我们还通过添加标记为该社区的属性 1 以及其他社区的零作为跟踪这些用户在哪里进行交互的方式来记录此评论回复发生在哪个社区。 对于此版本的代码,如果这些用户之前已经进行过交互,我们也会注意到,通过向表示发生新交互的社区的属性添加一个。

更新:

我从图中删除了自循环,但不幸的是仍然遇到相同的错误。

您似乎遇到了一个已知错误,但已更正。 更多细节在这里:

https://github.com/networkx/networkx/pull/4996

我认为它尚未出现在 networkx 的最新发布版本中(看起来它会出现在 2.7 版中),但是如果您将正在使用的算法替换为此处的代码: https : //github.com/ networkx/networkx/blob/main/networkx/algorithms/community/modularity_max.py它应该解决这个问题。

将 networkx 从 2.6.2 更新到 2.6.3 为我解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM