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?

3
  • For sliding max - stackoverflow.com/questions/43288542/…. For a generic one, get sliding windows and use ufunc along appropriate axis - stackoverflow.com/questions/40084931/… Commented Oct 29, 2020 at 8:59
  • can you just reshape the numpy array and move along the correct axis? np.max(input.reshape(4,400,5),axis=-1) Commented Oct 29, 2020 at 14:19
  • @MBeale Does np.reshape ensures the order like skimage.util.view_as_blocks ? Commented Nov 1, 2020 at 5:36

1 Answer 1

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]]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! it boost my process almost 20 times!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.