title: Find center of mass for image along axis
body: <p>I am trying to find a center of mass for each column of the image. I currently use <code>scipy.ndimage.measurements.center_of_mass</code> module and simple iteration to do that. My current code is: </p>
  
  <pre><code>def get_y_vec(img):
  
      out = []
  
      for col in img.T: 
          out.append(np.around(measurements.center_of_mass(col)[0], 1))
  
      out = np.array(out)
  
      return out
  </code></pre>
  
  <p>This works and produces pretty good results, however, it is pretty slow. Is there a more performant alternative to do achieve same output? </p>
  
  <p>EDIT:</p>
  
  <p>I just realised that center of mass is a fairly straightforward maths and can be written as such: </p>
  
  <pre><code>get_com = lambda m: np.round(np.sum(np.arange(m.shape[0])*m)/np.sum(m), 1)
  
  
  def get_y_vec(img):
  
      out = np.apply_along_axis(get_com, 0, img)
  
      return out
  </code></pre>
  
  <p>While it's 4x faster, my images are fairly large and I feel like we can come up with fully vectorized solution instead of using <code>np.apply_along_axis</code> or iteration.</p>