python-运行5000个循环而没有错误以获得样本中位数的样本分布

时间:2018-09-20 15:38:04

标签: python

如何重复此代码5000次?似乎无法使其正常工作。如何在不运行循环的情况下做到这一点?

age = ['<=20', '21-30', '31-40', '41-50', '51-60', '61-65', '>=66']
number_of_people = np.array([105770, 1170176, 1797410, 1256255, 692903, 88260, 7362])
mean_salary = np.array([17487, 32481, 47044, 56863, 59514, 76572, 114779])
sd = np.array([7459, 18207, 37225, 65919, 103232, 137916, 360839])
total_population = 5118136
samplesize = 200

sample200 = [(num/total_population) * samplesize for num in number_of_people]
sample200 = np.round(sample200)
#Adding one person to age group of '>=66' for sample size of 200
newsample200 = [4, 46, 70 , 49, 27, 3, 1]


sample200dist = np.array([])

for i in range(len(newsample200)):
    for j in range(newsample200[i]):
        new_dist = np.random.normal(mean_salary[i], sd[i])
        if new_dist > 0.0:
            sample200dist = np.append(sample200dist, new_dist)
        else:
            sample200dist = np.append(sample200dist, abs(new_dist))

print('The median monthly salary of sample size 200 is:', np.median(sample200dist))

0 个答案:

没有答案