I have a list of lists and I have to randomly choose one of them with weights.
I thought I could simply use np.random.choice
but it returns an error since the argument is not a 1-D array. Of course, one way to solve this issue is to randomly choose an integer number and then select the list in the following way:
import numpy as np
all_lists=[[1,2],[3,4]]
weights=np.array([1.,3.])
prob=weights/np.sum(weights)
index=np.random.choice(range(len(all_lists)),p=prob)
selected_list=all_lists[index]
print selected_list
but is there a more immediate way, without wasting time selecting the index first?
Aucun commentaire:
Enregistrer un commentaire