Interpolating 2 numpy arrays
<p>Is there any numpy or scipy or python function to interpolate between two 2D numpy array's? I have two 2D numpy arrays, and I want to apply changes to the first numpy array to make it similar to the second 2D array. The constraint is that I want the changes to be smooth. e.g., let the arrays be:</p>

<pre><code>A
[[1 1 1
  1 1 1
  1 1 1]]
</code></pre>

<p>and</p>

<pre><code>B
[[34  100 15
  62  17  87
  17  34  60]]
</code></pre>

<p>To make A similar to B, I could add 33 to the first grid cell of <code>A</code> and so on.. However, to make the changes smoother, I plan to compute a mean using a 2x2 window on array <code>B</code> and then apply the resulting changes to array <code>A</code>. Is there a built in numpy or scipy method to do this or follow this approach without using for loop.</p>

<p>One way of smoothing could be to use <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve2d.html" rel="nofollow">convolve2d</a>:</p>

<pre><code>import numpy as np
from scipy import signal

B = np.array([[34, 100, 15],
              [62,  17, 87],
              [17,  34, 60]])
kernel = np.full((2, 2), .25)
smoothed = signal.convolve2d(B, kernel)
# [[  8.5   33.5   28.75   3.75]
#  [ 24.    53.25  54.75  25.5 ]
#  [ 19.75  32.5   49.5   36.75]
#  [  4.25  12.75  23.5   15.  ]]
</code></pre>

<p>The above pads the matrix with zeros from all sides and then calculates the mean of each 2x2 window placing the value at the center of the window.</p>

<p>If the matrices were actually larger, then using a 3x3 kernel (such as <code>np.full((3, 3), 1/9)</code>) and passing <code>mode='same'</code> to <code>convolve2d</code> would give a smoothed <code>B</code> with its shape preserved and elements "matching" the original. Otherwise you may need to decide what to do with the boundary values to make the shapes the same again.</p>

<p>To move <code>A</code> towards the smoothed <code>B</code>, it can be set to a chosen affine combination of the matrices using standard arithmetic operations, for instance: <code>A = .2 * A + .8 * smoothed</code>.</p>

python numpy