python指定概率随机取值参考如下:
下面是利用 np.random.choice()指定概率取样的例子:
import numpy as np
np.random.seed(0)
p =([0.1, 0.0, 0.7, 0.2]
value= np.random.choice([0, 1, 12, 34], p = p)
12
这意味着你可以以下面的概率分布取到index所对应的数值:
P
(
i
n
d
e
x
=
0
)
=
0.1
,
P
(
i
n
d
e
x
=
1
)
=
0.0
,
P
(
i
n
d
e
x
=
12
)
=
0.7
,
P
(
i
n
d
e
x
=
34
)
=
0.2
P(index=0)=0.1, P(index=1)=0.0,P(index=12)=0.7,P(index=34)=0.2
P(index=0)=0.1,P(index=1)=0.0,P(index=12)=0.7,P(index=34)=0.2