taynaud / python-louvain

Louvain Community Detection
BSD 3-Clause "New" or "Revised" License
964 stars 200 forks source link

How to get two communities #5

Closed jmmcd closed 7 years ago

jmmcd commented 7 years ago

I'm trying to find the two communities in the Karate Club graph. I think I should be able to use partition_at_level but I must be missing something!

G = nx.karate_club_graph()

print(community.best_partition(G)) # we get 3 communities "by default"

dendrogram = community.generate_dendrogram(G)
print(community.partition_at_level(dendrogram, 0)) # we get 6 
print(community.partition_at_level(dendrogram, 1)) # we get 4
print(community.partition_at_level(dendrogram, 2)) # IndexError
taynaud commented 7 years ago

In general, modularity optimization with Louvain method on Karate Club Graph gives 4 communities. Having 2 communities give a lower modularity than 4, and thus maximizing modularity here will not work.

With Louvain Method, you cannot really specify the number of communities you want. You may try to play with the resolution parameters but it is more suited to get more communities than less.

dendrogram is a tree, and in your case I think its depth is 2, so you have an IndexError when you start to get level 2.

jmmcd commented 7 years ago

Thanks. Yes, I misunderstood the dendrogram -- I thought it was like the tree used in hierarchical clustering, which allows to choose the number of clusters by ascending.

That is good to hear that it is an expected result for Louvain with the Karate Club.

I tried the resolution parameter, and as you said it didn't solve the problem.

On 8 March 2017 at 09:29, Thomas Aynaud notifications@github.com wrote:

In general, modularity optimization with Louvain method on Karate Club Graph gives 4 communities. Having 2 communities give a lower modularity than 4, and thus maximizing modularity here will not work.

With Louvain Method, you cannot really specify the number of communities you want. You may try to play with the resolution parameters but it is more suited to get more communities than less.

dendrogram is a tree, and in your case I think its depth is 2, so you have an IndexError when you start to get level 2.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/taynaud/python-louvain/issues/5#issuecomment-284991581, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKQBnZ-qJ4WlJrb8Z_NXx_xBfiSiynCks5rjnUOgaJpZM4MWdwC .

-- Dr. James McDermott Lecturer in Business Analytics Programme Director, MSc in Business Analytics D209 UCD Michael Smurfit Graduate Business School College of Business, University College Dublin, Ireland. Phone +353 1 716 8031 http://jmmcd.net http://www.ucd.ie/cba/members/jamesmcdermott/

jeongwhanchoi commented 3 years ago

Thanks. Yes, I misunderstood the dendrogram -- I thought it was like the tree used in hierarchical clustering, which allows to choose the number of clusters by ascending. That is good to hear that it is an expected result for Louvain with the Karate Club. I tried the resolution parameter, and as you said it didn't solve the problem.

In my case, I tried the resolution parameter as 6 or 7 then it could be changed the number of clusters(partitions).