lundi 23 août 2021

How to efficiently use numpy random choice for varying weight list

I am trying to use choice() function to randomly choose numbers from a list whose weights are in a list of lists. That is for every row I want to generate one number. It is possible to do it with for loop like following,

from numpy.random import choice
W_list=np.array([0.9,0.1],
                [0.95,0.05],
                [0.85,0.15])
number_list=[]
for i in range(len(W_list)):
   number_list.extend(choice([0,1],size=1, p=W_list[i]).tolist())

the p parameter needs to 1D hence not possible to use p=W_list I was wondering if there's any way to do this more efficiently without using the for loop.




Aucun commentaire:

Enregistrer un commentaire