resize using fractional step in numpy

how can I modify the size of a numpy array with a non-integer step? that is: To convert a 3D array with size (64,64,64) into a 3D array for example with size (55,100,60), interpolating the values or using something like kron function

Upvotes: 1

Views: 385

Answers (1)

Joe Kington
Joe Kington

Reputation: 284830

How do you want to interpolate?

There's no "best", "right", or "normal" way to interpolate data... It all depends on your problem.

There are a large number of different ways to do what you want.

A starting point would be scipy.ndimage.zoom which can use spline, linear, or nearest-neighbor interpolation, and does exactly what you want.

Specifying order=0 will give nearest-neighbor interpolation, order=1 will give linear interpolation, and anything greater than 1 will give the specified order of spline interpolation. (The default is order=3, which is "cubic spline" interpolation.)

In your case, it would be:

new = zoom(data, (55/64.0, 100/64.0, 60/64.0), order=typeofinterpolationyouwant)

You'll also need to consider how you'd like the boundaries handled. The default is to treat any values outside the original array as 0. With spline interpolation, this commonly leads to strong artifacts near the edges of the array.

Upvotes: 3

Related Questions