mardi 25 février 2020

How to choose elements out of a matrix randomly weighted

I am pretty new to python and have some problems with Randomness.

I am looking for something similar then RandomChoice in Mathematica. I create a Matrix of dimension let's say 10x3 with random numbers greater 0. Let us call the total sum of every row s_i for i=0,...,9

Later I want to choose for every row 2 out of 3 elements (no repetition) with weighted probability s_ij/s_i So I need something like this but with weigthed propabilities

n=10
aa=np.random.uniform(1000, 2500, (n,3))
print(aa)
help=[0,1,2]
dd=np.zeros((n,2))
for i in range(n):
    cc=random.sample(help,2)
    dd[i,0]=aa[i,cc[0]]
    dd[i,1]=aa[i,cc[1]]
print(dd)

Here, additionally speed is an important factor since I will use it in an Montecarlo approach (that's the reason I switched from Mathematica to Python) and I guess, the above code can be improved heavily

Thanks in advance for any tipps/help




Aucun commentaire:

Enregistrer un commentaire