[SciPy-User] resample and aggregate a numpy array

Pietro peter.zamb at gmail.com
Wed Mar 26 10:19:38 EDT 2014


Dear All,

I would like to resample a numpy array using a function to aggregate
the subtiles.
I try to clarify the problem with a figure:

# defineed an array, like:
[[1, 1, 2, 2, 3],
 [1, 1, 2, 2, 3],
 [1, 1, 2, 2, 3],
 [1, 1, 2, 2, 3],
 [1, 1, 2, 2, 3]]

# I would like to split, like:

    +------+------+------+
    | 1, 1 | 2, 2 | 3    |
    | 1, 1 | 2, 2 | 3    |
    +------+------+------+
    | 1, 1 | 2, 2 | 3    |
    | 1, 1 | 2, 2 | 3    |
    +------+------+------+
    | 1, 1 | 2, 2 | 3    |
    |      |      |      |
    +------+------+------+

# and aggregate using a function like: np.sum, to get:
[[4, 8, 6],
 [4, 8, 6],
 [2, 4, 3]]

I wrote two functions:

resample_array that reshape the 2D array into a 3D array, and then I
can apply whatever function along axis=2;
resample that takes directly the function to be more efficient from a
memory point.

The code is available here: https://gist.github.com/zarch/9782938

Do you think that there is any better/efficient/faster approach? I’ve
looked on numpy/scipy functions but I was not able to find anything to
do this kind of things, have I missed something?

All the best!

Pietro



More information about the SciPy-User mailing list