I know that using Python's random.choices I can do this:
import random
array_probabilities = [0.5 for _ in range(4)]
print(array_probabilities) # [0.5, 0.5, 0.5, 0.5]
a = [random.choices([0, 1], weights=[1 - probability, probability])[0] for probability in array_probabilities]
print(a) # [1, 1, 1, 0]
How to make an numpy array of 0 and 1 based on a probability array?
Using random.choices is fast, but I know numpy is even faster. I would like to know how to write the same code but using numpy. I'm just getting started with numpy and would appreciate your feedback.
CodePudding user response:
One option:
out = (np.random.random(size=len(array_probabilities)) > array_probabilities).astype(int)
Example output:
array([0, 1, 0, 1])
CodePudding user response:
Your question got me wondering so I wrote a basic function to compare their timings. And it seems you are right! Timings change but only a little. Here you can see the code below and the output.
import numpy as np
import time
import random
def stack_question():
start=time.time()*1000
array_probabilities = [0.5 for _ in range(4)]
a = [random.choices([0, 1], weights=[1 - probability, probability])[0] for probability in array_probabilities]
end=time.time()
return (start-end)
def numpy_random_array():
start_time=time.time()*1000
val=np.random.rand(4,1)
end_time=time.time()
return (start_time-end_time)
print("List implementation ",stack_question())
print("Array implementation ",numpy_random_array())
The output:
List implementation 1665476650232.8433
Array implementation 1665476650233.9226
CodePudding user response:
probabilities = np.random.rand(1,10)
bools_arr = np.apply_along_axis(lambda x: 1 if x > 0.5 else 0, 1, [probabilities])