2

Suppose I have 3-d numpy array like this:

arr = np.random.randn(14,10,10)

I need to resize it to shape of (14, 20, 20). That means the first dimension is separately resized from (10, 10) to (20, 20) with interpolation method.

How could I do this?

yatu
  • 86,083
  • 12
  • 84
  • 139
coin cheung
  • 949
  • 2
  • 10
  • 25
  • Possible duplicate of [Interpolate a 3D array in Python](https://stackoverflow.com/questions/52094020/interpolate-a-3d-array-in-python) – Chris Mar 22 '19 at 09:29

1 Answers1

4

For these sort of tasks a handy tool is scipy.ndimage.interpolation.zoom

This will be resizing the array and interpolating it using spline interpolation. In order to use it you need to provide a zooming factor, which in this case should be [1,2,2]. This will be specifying that you want a zooming factor of 2 along the two last axis:

from scipy.ndimage import interpolation

arr = np.random.randn(14,10,10)
new_arr = interpolation.zoom(arr,[1,2,2])

print(new_arr.shape)
# (14, 20, 20)
yatu
  • 86,083
  • 12
  • 84
  • 139
  • What if I need many arrays to be resized to same shape? adjust according to the zoom ratio might bring little shape difference between different arrays. – coin cheung Mar 22 '19 at 09:54
  • You mean many arrays with different shapes? This ratio works for the specific shape of the example you proposed. In order to compute the zooming factor of interest to get a specific shape you need `shape_of_interest / len(x)`, this will be your zooming factor for a given array – yatu Mar 22 '19 at 10:05
  • ou mean many arrays with different shapes? This ratio works for the specific shape of the example you proposed. In order to compute the zooming factor of interest to get a specific shape you need `shape_of_interest / shape_of_current_array`, this will be your zooming factor for a given array – yatu Mar 22 '19 at 10:20