1

I have calculated the optimal number of networks for each node by using igraph, and here is the code I used.

import igraph
g = igraph.Graph.Read_Ncol('data.txt')
dendrogram = g.community_edge_betweenness()
clusters = dendrogram.as_clustering()
membership = clusters.membership

And now I would like to use the set_node_attributes function in networkX to tag each node with its number of communities. So if I run nx.get_node_attributes(g,'counts') it should produce

{123: 2,
 124: 3,
 125: 4 and so on} where "123" is a node and "2" is the count associated 

I am thinking to use a for loop here but am not sure how to get started.

EDITED:

membership
#output: 
[2,
 3,
 4]
0

1 Answer 1

1

I am assuming membership is a dictionary with nodes as keys and counts as value, then based on the version of networkx you are using (I am using v2.1), check the syntax of set_node_attributes, for version 2.1, it is set_node_attributes(G, values, name=None), so you simply do

nx.set_node_attributes(G, membership, 'counts')

print G[123]['count']
#output 2

Then use get_node_attributes to extract the same dictionary, like this

attribute_dict = nx.get_node_attributes(G, 'counts')

print attribute_dict[123]
#output : 2

Update : Assuming membership is a list of counts, then it will be in the same order as G.nodes(), so we can

node_list = list(G.nodes())

count_dict = { k:v for k,v in zip(node_list,membership)}

then do

nx.set_node_attributes(G, count_dict, 'counts')
Sign up to request clarification or add additional context in comments.

1 Comment

Guess 'membership' is not a dictionary here. Should I write a for loop to create one with nodes as keys and counts as value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.