我有点被困在我觉得被卡住了很愚蠢的事情上,所以来自一双新眼睛的任何建议都会有所帮助。我有一个包含 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]),.....希望我清楚,欢迎提供任何建议,谢谢。
1 回答
慕田峪4524236
TA贡献1875条经验 获得超5个赞
IUC:
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]
添加回答
举报
0/150
提交
取消