I need an efficient way to row standardize a sparse matrix.
Given
W = matrix([[0, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0],
[1, 0, 0, 0, 1, 0, 1, 0, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 1, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 0]])
row_sums = W.sum(1)
I need to produce...
W2 = matrix([[0. , 0.5 , 0. , 0.5 , 0. , 0. , 0. , 0. , 0. ],
[0.33, 0. , 0.33, 0. , 0.33, 0. , 0. , 0. , 0. ],
[0. , 0.5 , 0. , 0. , 0. , 0.5 , 0. , 0. , 0. ],
[0.33, 0. , 0. , 0. , 0.33, 0. , 0.33, 0. , 0. ],
[0. , 0.25, 0. , 0.25, 0. , 0.25, 0. , 0.25, 0. ],
[0. , 0. , 0.33, 0. , 0.33, 0. , 0. , 0. , 0.33],
[0. , 0. , 0. , 0.5 , 0. , 0. , 0. , 0.5 , 0. ],
[0. , 0. , 0. , 0. , 0.33, 0. , 0.33, 0. , 0.33],
[0. , 0. , 0. , 0. , 0. , 0.5 , 0. , 0.5 , 0. ]])
Where,
for i in range(9):
W2[i] = W[i]/row_sums[i]
I'd like to find a way to do this without loops (i.e. Vectorized) and using Scipy.sparse matrices. W could be as large at 10mil x 10mil.