I needed a little help with one of the simplest Python problems and I'm asking you to help me out. As a very simple and basic example, I added a small dataset and for each node I added two properties called "betweenness centrality" and "closeness centrality". Now, I am going to extract the largest feature value from the betweenness and closeness values defined as the feature of the node and select it as the influential node. But unfortunately the end result is not what I want:
import networkx as nx
G = nx.read_gml('./networks/karate.gml',label=None)
bb = nx.betweenness_centrality(G)
cc = nx.closeness_centrality(G)
nx.set_node_attributes(G,bb,"Betweenness")
nx.set_node_attributes(G,cc,"Closeness")
for g in G.nodes():
continue
print(max(G.nodes.data()),"\n")
The answer is as follows:
(34, {'Betweenness': 0.30407497594997596, 'Closeness': 0.55})
If I understood correctly, the correct answer should be as follows:
(1, {'Betweenness': 0.43763528138528146, 'Closeness': 0.5689655172413793})
What do you think about this? `
Because the "max" function is being called on "G.nodes.data()", it will actually return the node with the highest ID number rather than the node with the highest betweenness or closeness centrality.
The key argument specifies a lambda function that extracts the relevant attribute from each node's attribute dictionary for the comparison.