title: Efficient way to normalize a Scipy Sparse Matrix
body: <p>I'd like to write a function that normalizes the rows of a large sparse matrix (such that they sum to one).</p>
  
  <pre><code>from pylab import *
  import scipy.sparse as sp
  
  def normalize(W):
      z = W.sum(0)
      z[z &lt; 1e-6] = 1e-6
      return W / z[None,:]
  
  w = (rand(10,10)&lt;0.1)*rand(10,10)
  w = sp.csr_matrix(w)
  w = normalize(w)
  </code></pre>
  
  <p>However this gives the following exception:</p>
  
  <pre><code>File "/usr/lib/python2.6/dist-packages/scipy/sparse/base.py", line 325, in __div__
       return self.__truediv__(other)
  File "/usr/lib/python2.6/dist-packages/scipy/sparse/compressed.py", line 230, in  __truediv__
     raise NotImplementedError
  </code></pre>
  
  <p>Are there any reasonably simple solutions? I have looked at <a href="https://stackoverflow.com/questions/10475457/iterating-over-scipy-sparse-matrix-by-column">this</a>, but am still unclear on how to actually do the division.</p>