title: Numpy normalize multi dim (>=3) array
body: <p>I have a 5 dim array (comes from binning operations) and would like to have it <em>normed</em> (sum == 1 for the last dimension).</p>
  
  <p>I thought I found the answer <a href="https://stackoverflow.com/a/22546877/827818">here</a> but it says:
  <code>ValueError: Found array with dim 5. the normalize function expected &lt;= 2.</code></p>
  
  <p>I achieve the result with 5 nested loops, like:</p>
  
  <pre><code> for en in range(en_bin.nb):
      for zd in range(zd_bin.nb):
          for az in range(az_bin.nb):
              for oa in range(oa_bin.nb):
                  # reduce fifth dimension (en reco) for normalization
                  b = np.sum(a[en][zd][az][oa])
                  for er in range(er_bin.nb):
                      a[en][zd][az][oa][er] /= b
  </code></pre>
  
  <p>but I want to vectorise operations.</p>
  
  <p>For example:</p>
  
  <pre><code>In [18]: a.shape
  Out[18]: (3, 1, 1, 2, 4)
  In [20]: b.shape
  Out[20]: (3, 1, 1, 2)
  
  In [22]: a
  Out[22]:
  array([[[[[ 0.90290316,  0.00953237,  0.57925688,  0.65402645],
        [ 0.68826638,  0.04982717,  0.30458093,  0.0025204 ]]]],
  
  
  
     [[[[ 0.7973917 ,  0.93050739,  0.79963614,  0.75142376],
        [ 0.50401287,  0.81916812,  0.23491561,  0.77206141]]]],
  
  
  
     [[[[ 0.44507296,  0.06625994,  0.6196917 ,  0.6808444 ],
        [ 0.8199077 ,  0.02179789,  0.24627425,  0.43382448]]]]])
  
  In [23]: b
  Out[23]:
  array([[[[ 2.14571886,  1.04519487]]],
  
  
     [[[ 3.27895899,  2.33015801]]],
  
  
     [[[ 1.81186899,  1.52180432]]]])
  </code></pre>