gpt4 book ai didi

python - numpy.random 从给定列表中选择一个值,该值本身是从列表中选择的

转载 作者:行者123 更新时间:2023-12-01 08:35:54 24 4
gpt4 key购买 nike

我有点被困在一些事情上,我觉得自己被困在这件事上很愚蠢,所以来自新视野的任何建议都会有帮助。

我有一个包含 12 个年龄组的数组,大多数年龄组包含 4 个年龄组。

a1 = ([15,16,17,18,19])
a2=([20,21,22,23,24])
a3=([25,26,27,28,29])
a4=([30,31,32,33,34])
a5=([35,36,37,38,39])
a6=([40,41,42,43,44])
a7=([45,46,47,48,49])
a8=([50,51,52,53,54])
a9=([55,56,57,58,59])
a10=([60,61,62,63,64])
a11=([65,66,67,68,69])
a12=([70,71,72,73,74,75,76,77,78,79,80])
age=([a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12])

选择任何年龄组的概率为:

age_prob=[0.1, 0.125, .145,.13,.115,.1,.085,.075,.02,.06,.035,.01]

选择年龄组后,我想以相同的概率从该组中提取单个年龄。然后将该年龄添加到列中,直到包含 100 个年龄。这将为我生成一个年龄:

age_group=np.random.choice(age, p=age_prob)
Age=np.random.choice(age_group)
Age

但是如果我尝试创建 100 个年龄,我会得到 100 个年龄组的数组`

 age_group=np.random.choice(age, p=age_prob)
Age=np.random.choice(age_group,100)
Age

我尝试过这种方法一段时间:

age_indices = np.random.choice(len(age), 100, replace=True, p=age_prob)
Age=[age[i] for i in age_indices]
Age1=np.random.choice(Age,100)

但是把这个搞出来了

array([list([20, 21, 22, 23, 24]), list([45, 46, 47, 48, 49]),
list([40, 41, 42, 43, 44]), list([35, 36, 37, 38, 39]),
list([15, 16, 17, 18, 19]), list([25, 26, 27, 28, 29]),
list([40, 41, 42, 43, 44]), list([60, 61, 62, 63, 64]),
list([45, 46, 47, 48, 49]), list([30, 31, 32, 33, 34]),.....

希望我清楚,欢迎任何建议,谢谢。

`

最佳答案

IIUC:

l = []

for i in np.random.choice(age, p=age_prob, size=100):
l.append(np.random.choice(i))
print(l)

输出:

[21, 15, 35, 34, 53, 20, 48, 27, 29, 37, 56, 55, 24, 26, 52, 59, 54, 48, 29, 39, 35, 29, 54, 62, 79, 67, 46, 49, 50, 28, 69, 28, 24, 34, 24, 48, 29, 36, 34, 48, 21, 49, 55, 26, 17, 31, 22, 22, 20, 17, 62, 51, 29, 53, 20, 18, 32, 52, 23, 42, 64, 32, 60, 39, 34, 46, 48, 21, 50, 53, 34, 23, 31, 34, 77, 37, 20, 23, 23, 25, 79, 22, 23, 20, 38, 24, 54, 39, 34, 31, 32, 44, 24, 52, 44, 50, 46, 25, 28, 15]

关于python - numpy.random 从给定列表中选择一个值,该值本身是从列表中选择的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53730303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com