15

I'm studying about detection communities in networks.

I'm use igraph and Python

For the optimal number of communities in terms of the modularity measure:

from igraph import *
karate = Nexus.get("karate")
cl = karate.community_fastgreedy()
cl.as_clustering().membership

For supply the desired number of communities:

from igraph import *
karate = Nexus.get("karate")
cl = karate.community_fastgreedy()
k=2
cl.as_clustering(k).membership

However, I like to do this using networkx. I know get optimal number of communities in terms of the modularity measure:

import community # --> http://perso.crans.org/aynaud/communities/
import fastcommunity as fg # --> https://networkx.lanl.gov/trac/ticket/245
import networkx as nx

g = nx.karate_club_graph()
partition = community.best_partition(g)
print "Louvain Modularity: ", community.modularity(partition, g)
print "Louvain Partition: ", partition

cl = fg.communityStructureNewman(g)
print "Fastgreed Modularity: ", cl[0]
print "Fastgreed Partition: ", cl[1]

But I can not get the desired number of communities. Are there some algorithm for this, using Networkx?

SlowLoris
  • 995
  • 6
  • 28
Alan Valejo
  • 1,305
  • 3
  • 24
  • 44
  • This is a very recent work but is extremely useful: https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.117.078301 – Peaceful Sep 07 '16 at 14:28

2 Answers2

5

I'm also new to networkx and igraph, I used Gephi, an data visualization tool/software. And it has the same community detection algorithm as the one in networkx you are now using. Specifically, in http://perso.crans.org/aynaud/communities/

It uses the louvain method described in Fast unfolding of communities in large networks, Vincent D Blondel, Jean-Loup Guillaume, Renaud Lambiotte, Renaud Lefebvre, Journal of Statistical Mechanics: Theory and Experiment 2008(10), P10008 (12pp)

You can not get desired number of communities, as I know, there're two ways worth to try:

  • Use Gephi. You can use gephi and there's a parameter called resolution that would change the size of the community you get.
  • Use NetworkX. This time, we may not use best_partition(G) any more. But use partition_at_level(dendrogram, level) , I guess this might help.

Check the source code here for more info.

zihaolucky
  • 196
  • 1
  • 11
  • 3
    NetworkX doesn't have community detection. It's all coming from `community`, which builds upon NetworkX. More specifically, `best_partition()` is `community.best_partition()`. – MERose Apr 18 '16 at 12:35
2

Perhaps I am misunderstanding you, but if you would like the number of communities output by the NetworkX implementation of the best_partition algorithm, just note that best_partition(G) gives a dictionary with nodes as keys and their partition number as value.

You can count the number of unique values in a dictionary like this (likely not optimal):

dict = {'a':1,'b':1,'c':2,'d':1,'e':3,'f':4,'g':5}
count=list(set([i for i in dict.values()]))
print count
print len(count)

With result

[1, 2, 3, 4, 5]
5
Johannes Wachs
  • 1,270
  • 11
  • 15
  • 2
    I think the OP is interested in stating a priori the number of communities to detect, not in receiving the optimal number of communities – duhaime Jul 07 '15 at 17:43