简体   繁体   English

在Python Networkx中的两个节点之间添加按节点的边属性

[英]Adding an edge by node attribute between two nodes in Python Networkx

I'm writing some python code to create an edge between two nodes, and then randomly remove 2 edges from one of those nodes but with other nodes (not the original two). 我正在编写一些python代码在两个节点之间创建一条边,然后从其中一个节点中随机删除2条边,但与其他节点(不是原始的两个节点)一起删除。

Right now I'm stuck (and have gone over all networkx documentation multiple times). 现在,我被卡住了(并且已经遍历了所有networkx文档多次)。

I called/named one of the nodes "bad_apple_Bad_apple" - the other one I captured by it being the most central node in "self.most_central_node" using a max function to identify it. 我调用/命名了一个节点“ bad_apple_Bad_apple”-我捕获的另一个节点是“ self.most_central_node”中最中心的节点,并使用max函数对其进行了识别。 I can't seem to be able to select the node object itself so that I can create the edge using add_edge(). 我似乎无法选择节点对象本身,因此无法使用add_edge()创建边缘。

I'm pretty sure I'll need to explain more, but below is the code. 我敢肯定,我需要解释更多,但是下面是代码。 I can post more of the code if need be, but I tried to go a little further up the code file to give some background on what I'm doing. 如果需要,我可以发布更多代码,但我尝试进一步扩展代码文件,以提供一些背景信息。

    self.most_central_node = max(nx.closeness_centrality(self.combined_network))
    print("The node with the max value for closeness centrality in the combined network is: ", self.most_central_node)

    self.bad_apple_node_stuff = self.combined_network['bad_apple_Bad_apple']

    print("This is the bad apple test", self.bad_apple_node_stuff) #This is not printing the node object which means I'm not selecting it. It's printing it's attributes, I think. This is where the issue is.

    self.combined_network.add_edge(self.combined_network.node['bad_apple_Bad_apple'], self.combined_network.node['closeness'==self.most_central_node]) #<<-- this doesnt work, no specific error though

Your code has a few problems. 您的代码有一些问题。 In the line: 在该行中:

self.most_central_node = max(nx.closeness_centrality(self.combined_network))

closeness_centrality actually returns a dictionary where the nodes are keys and their centralities are the values. closeness_centrality实际上返回一个字典,其中节点是键,而它们的中心是值。 Your line simply chose the maximum node by either the nodes being integers or longest string or whatever. 您的行只是通过整数或最长字符串之类的节点来选择最大节点。 You should change that to: 您应该将其更改为:

close = nx.closeness_centrality(self.combined_network)
from operator import itemgetter
self.most_central_node = max(close.items(), key=itemgetter(1))[0]

That last line compares all (node, closeness) pairs by their closeness value and then simply stores the associated node in the variable. 最后一行通过它们的接近度值比较所有(节点,接近度)对,然后将关联的节点简单地存储在变量中。

Now, if you already know that your other node is 'bad_apple_Bad_apple' , then you can simply issue the statement: 现在,如果您已经知道另一个节点是'bad_apple_Bad_apple' ,则只需发出以下语句:

self.combined_network.add_edge(self.most_central_node, 'bad_apple_Bad_apple')

Please also note that you have accessed dictionaries in your code that store attributes: 另请注意,您已经在代码中访问了存储属性的字典:

network.graph

is where graph attributes are stored, for example, name . 是图形属性的存储位置,例如name

network.node

is where node attributes are stored, for example, 例如,存储节点属性的位置

network.node['bad_apple_Bad_apple']

will return a dict with the names of the attributes and their values. 将返回带有属性名称及其值的dict

You say that you have read the documentation multiple times but I strongly suggest that you go through the tutorial . 您说您已经阅读了多次文档,但是我强烈建议您阅读本教程

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

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