title: resize with averaging or rebin a numpy 2d array
body: <p>I am trying to reimplement in python an IDL function:</p>
  
  <p><a href="http://star.pst.qub.ac.uk/idl/REBIN.html" rel="noreferrer">http://star.pst.qub.ac.uk/idl/REBIN.html</a></p>
  
  <p>which downsizes by an integer factor a 2d array by averaging.</p>
  
  <p>For example:</p>
  
  <pre><code>&gt;&gt;&gt; a=np.arange(24).reshape((4,6))
  &gt;&gt;&gt; a
  array([[ 0,  1,  2,  3,  4,  5],
         [ 6,  7,  8,  9, 10, 11],
         [12, 13, 14, 15, 16, 17],
         [18, 19, 20, 21, 22, 23]])
  </code></pre>
  
  <p>I would like to resize it to (2,3) by taking the mean of the relevant samples, the expected output would be:</p>
  
  <pre><code>&gt;&gt;&gt; b = rebin(a, (2, 3))
  &gt;&gt;&gt; b
  array([[  3.5,   5.5,  7.5],
         [ 15.5, 17.5,  19.5]])
  </code></pre>
  
  <p>i.e. <code>b[0,0] = np.mean(a[:2,:2]), b[0,1] = np.mean(a[:2,2:4])</code> and so on.</p>
  
  <p>I believe I should reshape to a 4 dimensional array and then take the mean on the correct slice, but could not figure out the algorithm. Would you have any hint?</p>