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],
with a bit of matrix algebra
>>> cc
<9x9 sparse matrix of type '<type 'numpy.int32'>'
with 24 stored elements in Compressed Sparse Row format>
>>> ccd = sparse.spdiags(1./cc.sum(1).T, 0, *cc.shape)
>>> ccn = ccd * cc
>>> np.round(ccn.todense(), 2)
array([[ 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. ]])
>>> ccn
<9x9 sparse matrix of type '<type 'numpy.float64'>'
with 24 stored elements in Compressed Sparse Row format>