Reputation: 137
I have a 1D numpy array which contains measured data. I want to shrink this array from e.g. 2543 data points to 2000 in order to compare it with other results. How can I shrink my 1D array while keeping the order. The best idea would be to somehow select every n-th entry but my measured data always produces arrays with different lenghts. I thought of numpy.random.choice but it's not keeping the original order and I would prefer an approach which is not random.
Upvotes: 1
Views: 510
Reputation: 475
I think this code solves your problem:
import numpy as np
# Let's assume that this is your numpy array
a = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
# ...and this is the final array size
n = 3
if n < a.size:
# Randomly select some indices to remove (without repetitions)
indices_to_remove = np.random.choice(a.size, a.size - n, replace=False)
# Remove elements with these indices from your array
new_a = np.delete(a, indices_to_remove)
else:
# In this case there is nothing to remove because the array is too small
new_a = a
print(a, new_a)
Example output:
[10 20 30 40 50 60 70 80 90 100] [20 30 90]
Upvotes: 1