重抽样结果的可视化
现在,您将把上一练习中的模拟结果可视化!您将继续使用 nba_weights,其中包含一组 NBA 球员的体重(单位:千克):
nba_weights = [96.7, 101.1, 97.9, 98.1, 98.1,
100.3, 101.0, 98.0, 97.4]
以下是您在上一练习中使用的模拟代码:
simu_weights = []
for i in range(1000):
bootstrap_sample = random.choices(nba_weights, k=9)
simu_weights.append(np.mean(bootstrap_sample))
mean_weight = np.mean(simu_weights)
upper = np.quantile(simu_weights, 0.975)
lower = np.quantile(simu_weights, 0.025)
print(mean_weight, lower, upper)
上一练习中生成的 simu_weights 列表已为您加载。同样,mean_weight、lower 和 upper 也已定义,分别对应均值以及 2.5% 和 97.5% 分位数,构成您的置信区间。
以下包已为您加载:random、将 numpy 导入为 np、将 seaborn 导入为 sns,以及将 matplotlib.pyplot 导入为 plt。
本练习是课程的一部分
Python 中的蒙特卡洛模拟
练习说明
- 使用
sns.displot()绘制模拟体重的分布。 - 使用
plt.axvline()绘制 95% 置信区间的两条垂直线(按lower再upper的顺序),颜色为红色;同时用绿色绘制均值。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Plot the distribution of the simulated weights
____
# Plot vertical lines for the 95% confidence intervals and mean
plt.axvline(____, color="red")
plt.axvline(____, color="red")
plt.axvline(____, color="green")
plt.show()