有放回抽样
自助法(bootstrapping)非常适合用来计算均值的置信区间;现在请您来练习一下!
nba_weights 包含一组 NBA 球员的体重(单位:千克):
nba_weights = [96.7, 101.1, 97.9, 98.1, 98.1,
100.3, 101.0, 98.0, 97.4]
您希望使用该列表来计算 NBA 球员平均体重的 95% 置信区间。
以下内容已为您导入:random,以及将 numpy 导入为 np。
本练习是课程的一部分
Python 中的蒙特卡洛模拟
练习说明
- 使用
random.choices()进行有放回抽样:每次从列表中抽取 9 个样本,共重复 1,000 次。 - 计算模拟结果的均值和 95% 置信区间,并将置信区间的下界赋给
lower,上界赋给upper。
交互式实操练习
通过完成这段示例代码来试试这个练习。
simu_weights = []
# Sample nine values from nba_weights with replacement 1000 times
for i in range(____):
bootstrap_sample = ____
simu_weights.append(np.mean(bootstrap_sample))
# Calculate the mean and 95% confidence interval of the mean for your results
mean_weight = ____
upper = ____
lower = ____
print(mean_weight, lower, upper)