Home > Back-end >  How to create a NumPy array of booleans with k% True values?
How to create a NumPy array of booleans with k% True values?

Time:10-28

I know that we can create a NumPy array of boolean values with the following line of code:

np.random.choice(a=[False, True], size=(N,))

But what if I want to specify that I want this random array to have around 60% (or more generally k%) True values?

CodePudding user response:

Using np.random.choice can be really slow for large arrays. I recommend doing

import numpy as np
N = 100_000
booleans = np.random.rand(N) < 0.6

np.random.rand will produce uniform random numbers between 0.0 and 1.0, and the comparison will set all numbers under 0.6 to True, giving you an array with roughly 60% True values.

If you need exactly the proportion you're asking for, you can do this

k = 60  # 60%
booleans = np.zeros(shape=(N,), dtype=bool) # Array with N False
booleans[:int(k / 100 * N)] = True  # Set the first k% of the elements to True
np.random.shuffle(booleans)  # Shuffle the array

np.count_nonzero(booleans)  # Exactly 60 000 True elements

CodePudding user response:

Use the probabilities array argument of np.choice

import numpy as np

N = 100
res = np.random.choice(a=[False, True], size=(N,), p=[0.4, 0.6])
print(np.count_nonzero(res))

Output (of a single run)

62
  • Related