0

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?

1
  • 1
    choice take a p value, so you can set p=[0.4, 0.6] Commented Oct 27, 2021 at 14:42

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

3

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

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.