title: Numpy percentages along axis in 2d array
body: <p>I have a matrix of counts, </p>
  
  <pre><code>import numpy as np
  x = np.array([[ 1,2,3],[1,4,6],[2,3,7]])
  </code></pre>
  
  <p>And I need the percentages of the total along axis = 1: </p>
  
  <pre><code>for i in range(x.shape[0]):
      for j in range(x.shape[1]):
          x[i,j] = x[i,j] / np.sum(x[i,:])
  </code></pre>
  
  <p>In numpy broadcast form.  </p>
  
  <p>Currently, I have: </p>
  
  <pre><code>x_sums = np.sum(x,axis=1)
  for j in range(x.shape[1]):
       x[:,j] = x[:,j] / x_sums[:]
  </code></pre>
  
  <p>Which puts most of the complexity in numpy code...but a numpy one liner would be best. </p>
  
  <p>Also, </p>
  
  <pre><code>def percentages(a):
      return a / np.sum(a)
  
  x_percentages = np.apply_along_axis(percentages,1,x)
  </code></pre>
  
  <p>But that still involves python.  </p>
  
  <hr>
  
  <pre><code> np.linalg.norm
  </code></pre>
  
  <p>Is very close, in terms of what is going on, but they only have the 8 hardcoded norms, which does not include percentage of total. </p>
  
  <p>Then there is <code>np.percentile</code>, which is again close...but it is computing the sorted percentile. </p>