 # Get the block matrix (for a community detection algorithm) in python igraph?

I was working with graph-tool and I was able to use

``````state = gt.minimize_blockmodel_dl(g)
e = state.get_matrix()
plt.matshow(e.todense())
``````

I would like to obtain the same in igraph for any of the community detection algorithms. Up to now I can get the membership of each vertex, but can’t find the block matrix (which is of course different to the adjacency matrix).

Thanks!

Cross-posted on StackOverlow

Cross-posting is fine, but please always link the posts together, so people won’t waste their time trying to answer it on one forum if it is already answered on the other. BTW the reason why the system withheld this post for moderation is that it was pasted and posted in a matter of seconds, with no time spent to edit it.

Can you explain in plain words (instead of graph-tool code) what you want to achieve? I do not know what that function does.

If I understand correctly, you would like to have the adjacency matrix, but with the rows and columns permuted according to the resulting cluster membership, correct?

You can get the adjacency matrix by `g.get_adjacency`. This returns an `igraph` matrix. It is easier typically to work with a `numpy` matrix (here imported as `np`), which you can get as follows:

``````A_mat = g.get_adjacency()
A = np.array(list(A_mat))
``````

You can then reorder this matrix however you prefer. If you want to reorder it based on the clustering results, you can do that as follows, where `cluster` is assumed to be the result of any clustering algorithm:

``````idx = np.argsort(cluster.membership)
A = A[np.ix_(idx, idx)]
``````

This is already a dense matrix, so you can simply plot this using `plt.matshow`.