2

Is there a 'pythonic' way to cleanly down-sample without multiple for loops?

This example below is the type of for loop I wish to get rid of.

Minimum working example:

import numpy as np
unsampled_array = [1,3,5,7,9,11,13,15,17,19]
number_of_samples = 7
downsampled_array = []
downsampling_indices = np.linspace(0, len(unsampled_array)-1, number_of_samples).round()
for index in downsampling_indices:
    downsampled_array.append(unsampled_array[int(index)])
print(downsampled_array)

Result:

>>> [ 1  5  7  9 13 17 19]

2 Answers 2

3

If you want "real" downsampling, where each value is the mean of k values, you can use

unsampled_array.reshape(-1, k).mean(1) 

Make sure unsampled_array is a np.array. In your case, k=2. That will give you:

[ 2. 6. 10. 14. 18.]

* Update: If you just want to take the first item for each k items, you can use this code:

unsampled_array.reshape(-1, 2)[:, 0]

Take a look at this plot:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

You are correct. I didn't mention it in the question but I was initially interested in downsampling objects and downsampling between and to sample sizes. I guess I over simplified the example.
2

You need function np.ix_, as follows:

import numpy as np


unsampled_array = np.array([1,3,5,7,9,11,13,15,17,19])
number_of_samples = 5
downsampling_indices = np.linspace(0, len(unsampled_array)-1, number_of_samples).round()
downsampling_indices = np.array(downsampling_indices, dtype=np.int64)

indices = np.ix_(downsampling_indices)
downsampled_array = unsampled_array[indices]

print(downsampled_array)

8 Comments

I don't think the np.ix_ is necessary at all. You can just go unsampled_array[ downsampling_indices].
This is great as it also works with non integer down sampling ( ie 10->7 )
np.ix_ is very quick. You can compare both approaches.
Instead of calling np.array with dtype=np.int64, add to the end of the np.linspace command astype(int). Also, instead of using round, I would use np.rint.
@NoamPeled You are right about astype(int) but np.rint returns a float for some reason so there is no benefit of np.rint(x).astype(int) over x.round().astype(int)
|

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.