0

I have the task of selecting p% of elements within a given numpy array. For example,

# Initialize 5 x 3 array-
x = np.random.randint(low = -10, high = 10, size = (5, 3))

x
'''
array([[-4, -8,  3],
       [-9, -1,  5],
       [ 9,  1,  1],
       [-1, -1, -5],
       [-1, -4, -1]])
'''

Now, I want to select say p = 30% of the numbers in x, so 30% of numbers in x is 5 (rounded up).

Is there a way to select these 30% of numbers in x? Where p can change and the dimensionality of numpy array x can be 3-D or maybe more.

I am using Python 3.7 and numpy 1.18.1

Thanks

1
  • What shape do you expect for the output array? Commented Mar 11, 2020 at 1:02

3 Answers 3

1

You can use np.random.choice to sample without replacement from a 1d numpy array:

p = 0.3
np.random.choice(x.flatten(), int(x.size * p) , replace=False)

For large arrays, the performance of sampling without replacement can be pretty bad, but there are some workarounds.

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

Comments

0

You can randome choice 0,1 and usenp.nonzero and boolean indexing:

np.random.seed(1)
x[np.nonzero(np.random.choice([1, 0], size=x.shape, p=[0.3,0.7]))]

Output:

array([ 3, -1,  5,  9, -1, -1])

Comments

0

I found a way of selecting p% of numpy elements:

p = 20

# To select p% of elements-
x_abs[x_abs < np.percentile(x_abs, p)]

# To select p% of elements and set them to a value (in this case, zero)-
x_abs[x_abs < np.percentile(x_abs, p)] = 0

Comments

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.