简体   繁体   English

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

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

I keep getting the above error when trying to run the greedy_modularity_communities community-finding algorithm from NetworkX on a network of 123212 nodes and 329512 edges.尝试在包含 123212 个节点和 329512 个边的网络上运行来自 NetworkX 的greedy_modularity_communities社区查找算法时,我不断收到上述错误。

simpledatasetNX here is a NetworkX Graph object.这里的 simpledatasetNX 是一个 NetworkX Graph 对象。 Here is what I most recently ran:这是我最近运行的:

greedy_modularity_communities(simpledatasetNX)

and what has been output:以及输出的内容:

---------------------------------------------------------------------------
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'

I've been running into this exact error several times after multiple attempts to remedy this.在多次尝试解决此问题后,我多次遇到此确切错误。 Here's where I started, and the things I did to fix it:这是我开始的地方,以及我为修复它所做的事情:

  1. First, I had constructed a NetworkX MultiDiGraph object from a data set, and I needed to turn this into a Graph object in order to run this algorithm.首先,我从数据集构建了一个 NetworkX MultiDiGraph对象,我需要将其转换为Graph对象才能运行该算法。 I looked at simply doing我看着简单地做

desired_graph_object = nx.Graph(multidigraphobject)

and it seemed to do what I wanted it to do on a much simpler test graph I made, so I did this and then tried to run the algorithm, getting the above error.它似乎在我制作的更简单的测试图中完成了我想要它做的事情,所以我这样做了,然后尝试运行算法,得到上述错误。

  1. I was confused about what the issue could be.我对这个问题可能是什么感到困惑。 Then I remembered that the way I constructed my multidigraph object involved a lot of attributes on the edges (there were closer to 500,000 edges on this original object) so that when I tried to simplify it down to a graph object, something wonky probably happened when more than one edge between the same two nodes was combined as I hadn't done anything to account for this.然后我想起我构建多向图对象的方式涉及边缘上的许多属性(这个原始对象上有接近 500,000 条边),所以当我试图将它简化为图形对象时,可能会发生一些奇怪的事情由于我没有做任何事情来说明这一点,因此将相同的两个节点之间的一条边合并在一起。 Some of these attributes were ints and some were strings, for example.例如,这些属性中的一些是整数,一些是字符串。 This seemed like a simple enough fix: all I had to do was construct a new graph object from my dataset and keep only a single attribute for analysis related to the greedy_modularity_communities algorithm.这似乎是一个足够简单的解决方法:我所要做的就是从我的数据集中构建一个新的图形对象,并只保留一个属性用于与 greedy_modularity_communities 算法相关的分析。 So I did that and ended up with the exact same error.所以我这样做了,最终得到了完全相同的错误。

I now suspect that there is something wrong with the way I constructed both of these graphs as the nature of the error is the same whether I run the algorithm on the simplified multidigraph object or on the graph object.我现在怀疑我构建这两个图的方式有问题,因为无论我在简化的多向图对象还是在图对象上运行算法,错误的性质都是相同的。

Here is my code for constructing the graph object;这是我构建图形对象的代码; the code for constructing the multidigraph object was essentially copy-pasted with minor adjustments to make this, so I don't feel the need to include it.构造多向图对象的代码基本上是复制粘贴的,并进行了细微的调整以实现这一点,所以我觉得没有必要包含它。 As far as I can tell, both the graph object this code constructs and the multidigraph object discussed previously look how I intended them to.据我所知,此代码构建的图形对象和前面讨论的多向图对象都符合我的预期。

%%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

Some additional context: my original dataset is a massive data frame that I wanted to construct my network from.一些额外的上下文:我的原始数据集是一个庞大的数据框,我想从中构建我的网络。 Each row represents a comment or post on a social media site.每行代表社交媒体网站上的评论或帖子。 It involves linking id's of comments to the parent_id's of comments that reply to the first comment.它涉及将评论的 id 链接到回复第一条评论的评论的 parent_id。 The link_id_dataframe_dict is a dictionary where a key is a given thread and the object associated with that key is a subdataframe of all the comments in that thread (ie, with that link_id). link_id_dataframe_dict 是一个字典,其中一个键是给定的线程,与该键关联的对象是该线程中所有评论的子数据帧(即,具有该 link_id)。

The idea is that we go through our entire data frame row by row, identify the thread/link_id that this row/comment is part of, then we search through the associated link_id data frame for the other row/comment that the row/comment we are considering is a response to.这个想法是我们逐行浏览整个数据帧,确定该行/评论所属的线程/link_id,然后我们在关联的link_id数据帧中搜索该行/评论所在的另一行/评论正在考虑是回应。 When we do so, we add an edge between two nodes, where this edge represents the comment, and the two nodes are the users who posted the reply and the comment being replied to.当我们这样做时,我们在两个节点之间添加一条边,这条边代表评论,这两个节点是发布回复的用户和被回复的评论。 We also make a note of which community this comment reply took place in by adding attribute 1 labeled with that community, and a zero for the other community as a way of keeping track of where these users are interacting.我们还通过添加标记为该社区的属性 1 以及其他社区的零作为跟踪这些用户在哪里进行交互的方式来记录此评论回复发生在哪个社区。 For this version of the code, if these users have interacted before, we note that as well by adding one to the attribute representing the community in which the new interaction has taken place.对于此版本的代码,如果这些用户之前已经进行过交互,我们也会注意到,通过向表示发生新交互的社区的属性添加一个。

UPDATES:更新:

I removed the self-loops from the graph yet still run into the same error unfortunately.我从图中删除了自循环,但不幸的是仍然遇到相同的错误。

It looks like you've encountered a known bug which has been corrected.您似乎遇到了一个已知错误,但已更正。 More details are here:更多细节在这里:

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

I don't think it's yet in the most recent released version of networkx (it looks like it will appear in version 2.7), but if you replace the algorithm you're using with the code here: https://github.com/networkx/networkx/blob/main/networkx/algorithms/community/modularity_max.py it should fix this.我认为它尚未出现在 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.

相关问题 AttributeError: 'NoneType' object 在写入文件时没有属性 'get' - AttributeError: 'NoneType' object has no attribute 'get' when writing into a file AttributeError:当我保存在age中时,“ NoneType”对象没有属性“ get” - AttributeError: 'NoneType' object has no attribute 'get' when I save in amage AttributeError: 'NoneType' object 在 Python 3 中没有属性 'get' 使用 beautifulsoup - AttributeError: 'NoneType' object has no attribute 'get' in Python 3 using beautifulsoup 从列中的字典获取值。 AttributeError: 'NoneType' object 在值为“None”时没有属性“get” - Get values from a dict in an column. AttributeError: 'NoneType' object has no attribute 'get' when a value is "None" AttributeError: 'NoneType' object 没有属性 'get' helpp - AttributeError: 'NoneType' object has no attribute 'get' helpp 'NoneType' object 没有属性 'get':AttributeError - 'NoneType' object has no attribute 'get': AttributeError AttributeError: &#39;NoneType&#39; 对象在使用 Gym 时没有属性 &#39;flip&#39; - AttributeError: 'NoneType' object has no attribute 'flip' when using gym AttributeError: 'NoneType' object 在使用 BeautifulSoup 时没有属性 'text' - AttributeError: 'NoneType' object has no attribute 'text' when using BeautifulSoup 接收 AttributeError: 'NoneType' object 使用纸浆时没有属性 'actualSolve' - Receiving AttributeError: 'NoneType' object has no attribute 'actualSolve' when using pulp Flasgger AttributeError:&#39;NoneType&#39;对象没有属性&#39;get&#39;吗? - Flasgger AttributeError: 'NoneType' object has no attribute 'get'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM