'How to get a group matrix from an adjacent matrix?
I have a large matrix F, indicating whether i and j are friends by F_ij == 1 (F_ii = 1 as well). From this matrix, I want to get a matrix G, indicating whether i and j are in the same group of friends (i.e. i and j are friends, or friends of friends, or friends of friends of friends, or ...).
I thought an efficient way is to prepare F with a sparse matrix, and multiply it until it converges like the sample code below.
from scipy.sparse import csr_matrix
# prepare individual links
row = [0,1,2,3,4,5,6,7,8,9,10,11,4,5,1,2]
col = [0,1,2,3,4,5,6,7,8,9,10,11,5,4,2,1]
data = [1] * len(row)
# F as sparse matrix
F = csr_matrix((data, (row, col)))
# Obtain G by convergence
for x in range(100000):
F_old = F
F = F_old * F_old
print(x)
if (F!=F_old).nnz==0:
print('converged')
break
G = F
print(G)
G seems correct. However, when I use this algorithm for my real data with >10000 individuals, it is very slow. Well, it is not so slow at the beginning (x=1,2,3...), but it is almost frozen when x comes around 6 or 7 and never converges. Do you know any efficient way to obtain G by fixing the code or using some packages?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
