Some simple fake data:
a = np.arange(9.).reshape(3,3)
s = sparse.csr_matrix(a)
To get the norm of each row from the sparse, you can use:
np.sqrt(s.multiply(s).sum(1))
And the renormalized s
would be
s.multiply(1/np.sqrt(s.multiply(s).sum(1)))
or to keep it sparse before renormalizing:
s.multiply(sparse.csr_matrix(1/np.sqrt(s.multiply(s).sum(1))))
To get ordinary matrix or array from it, use:
m = s.todense()
a = s.toarray()
If you have enough memory for the dense version, you can get the norm of each row with:
n = np.sqrt(np.einsum('ij,ij->i',a,a))
or
n = np.apply_along_axis(np.linalg.norm, 1, a)
To normalize, you can do
an = a / n[:, None]
or, to normalize the original array in place:
a /= n[:, None]
The [:, None]
thing basically transposes n
to be a vertical array.