Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weighted modularity not correct? #41

Closed
dafnevk opened this issue Aug 13, 2018 · 3 comments
Closed

Weighted modularity not correct? #41

dafnevk opened this issue Aug 13, 2018 · 3 comments
Assignees
Labels

Comments

@dafnevk
Copy link

dafnevk commented Aug 13, 2018

I noticed louvain-igraph gave different modularity values than I expected, and I found a small example where it seems the package is not taking the weights into account:

import igraph
import louvain
import numpy as np

A = np.array([[ 0.,  2.,  2.,  0.,  0.,  0.],
       [ 2.,  0.,  2.,  0.,  0.,  0.],
       [ 2.,  2.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  2.,  2.],
       [ 0.,  0.,  0.,  2.,  0.,  2.],
       [ 0.,  0.,  0.,  2.,  2.,  0.]])

g = igraph.Graph()
g.add_vertices(len(A))
ix, jx = A.nonzero()
for i,j in zip(ix, jx):
    if i<j:
        g.add_edge(i, j, weight=A[i,j])

part = louvain.find_partition(g, louvain.ModularityVertexPartition, weights='weight')
print(part.membership)
print(part.modularity)

Gives output:

[0, 0, 0, 1, 1, 1]
0.357142857142857

With my own calculations, as well as through other implementations, I find an (optimal) modularity value of 0.423 for this partiton. The modularity value of 0.357 is valid for the non-weighted equivalent of the graph.

Am I overlooking something or is this a bug in the code?

@vtraag
Copy link
Owner

vtraag commented Aug 13, 2018

Thanks, I'll look into it! The code is tested on weighted graphs, so in principle it should work. I'm not sure what is going on here, I'll have to take a closer look.

@vtraag vtraag self-assigned this Aug 14, 2018
@vtraag vtraag added the bug label Aug 14, 2018
@vtraag
Copy link
Owner

vtraag commented Aug 14, 2018

Initially, I overlooked it, but you are looking at the modularity that is provided by igraph by default. This is indeed an unweighted variant. To get the quality value of the partition, you should call part.quality(), which does provide the corrected weighted modularity. The confusion is understandable, I'll see if I can remove the modularity variable in some way.

@vtraag
Copy link
Owner

vtraag commented Aug 14, 2018

The modularity variable cannot be removed easily, and it would also create an inconsistency with the existing API from igraph, so I am leaving it as is. So, to get the quality of the partition, you always have to call partition.quality(), also when using louvain.ModularityVertexPartition.

@vtraag vtraag closed this as completed Aug 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants