mercredi 4 novembre 2020

Getting an array from np random choice avoiding loops and error "a must be 1-dimensional"

I'm trying to find a way to get an array of values from np.random.choise, using a matrix of values and one of probabilities, without using loops.

Imagine I have something like this

vals=  array([[ 0.        ,  1.22222222,  2.44444444,  3.66666667,  4.88888889,
         6.11111111,  7.33333333,  8.55555556,  9.77777778, 11.        ],
       [ 3.        ,  8.22222222, 13.44444444, 18.66666667, 23.88888889,
        29.11111111, 34.33333333, 39.55555556, 44.77777778, 50.        ]])

probs= array([[0.01056171, 0.15521083, 0.07796945, 0.09986356, 0.14516427,
        0.12496125, 0.00091384, 0.19739258, 0.00088116, 0.18708136],
       [0.01220221, 0.17791623, 0.13682813, 0.05679157, 0.16599396,
        0.09769565, 0.09365478, 0.15176203, 0.0965629 , 0.01059253]])

how can I get an array equivalent to this

[np.random.choice(vals[i],p=probs[i]) for i in range(len(probs))]

out[1]: 
[6.111111111111112, 23.88888888888889]

without using the for loop??

I was expecting np.random.choice to broadcast the matrices by row, but I get the error "a must be 1-dimensional".




Aucun commentaire:

Enregistrer un commentaire