简体   繁体   中英

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).

Right now I'm stuck (and have gone over all networkx documentation multiple times).

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. I can't seem to be able to select the node object itself so that I can create the edge using 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. 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:

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 .

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.

You say that you have read the documentation multiple times but I strongly suggest that you go through the tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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