1

I have an (4, 2000) numpy array and want to resample each column (N=4) for every 5 elements with such as max, min, left, right, which makes its shape as (4, 400).

I can do with Pandas.DataFrame using .resample('5Min').agg(~) or with numpy array and for loop like result = [max(input[i:i+5]) for i in range(0, len(input), 5)]. However, it takes amount of time with large input array since it's not vectorized. Is there any way that I can do with vectorized computation with np array?

Ehsan
  • 12,072
  • 2
  • 20
  • 33
Mark Yoon
  • 330
  • 4
  • 17
  • For sliding max - https://stackoverflow.com/questions/43288542/max-in-a-sliding-window-in-numpy-array. For a generic one, get sliding windows and use ufunc along appropriate axis - https://stackoverflow.com/questions/40084931/taking-subarrays-from-numpy-array-with-given-stride-stepsize – Divakar Oct 29 '20 at 08:59
  • can you just reshape the numpy array and move along the correct axis? `np.max(input.reshape(4,400,5),axis=-1)` – MBeale Oct 29 '20 at 14:19
  • @MBeale Does `np.reshape` ensures the order like `skimage.util.view_as_blocks` ? – Mark Yoon Nov 01 '20 at 05:36

1 Answers1

0

Here is another way that uses numpy strides under the hood (a is your array):

from skimage.util import view_as_blocks
a = view_as_blocks(a, (4,5))

Now, you can use methods/slicing for parameters you want:

#max
a.max(-1)[0].T
#min
a.min(-1)[0].T
#left
a[...,0][0].T
#right
a[...,-1][0].T

example:

a
#[[ 0  1  2  3  4  5  6  7  8  9]
# [10 11 12 13 14 15 16 17 18 19]
# [20 21 22 23 24 25 26 27 28 29]
# [30 31 32 33 34 35 36 37 38 39]]

output for max
#[[ 4  9]
# [14 19]
# [24 29]
# [34 39]]
Ehsan
  • 12,072
  • 2
  • 20
  • 33