可视化自助法(Bootstrap)
延续本课前面的内容,咱们来可视化通过自助法重采样估计得到的速度分布。我们对每个重采样样本都做了最小二乘直线拟合来估计斜率,用以检验斜率估计的变动与不确定性。
为了帮助您起步,我们已预加载函数 compute_resample_speeds(distances, times),用于生成速度的样本分布。

本练习是课程的一部分
Python 线性建模入门
练习说明
- 使用预定义的
compute_resample_speeds(distances, times)计算resample_speeds。 - 使用
np.mean()从resample_speeds计算speed_estimate。 - 使用
np.percentile()并传入[5, 95]来计算resample_speeds的percentiles,作为置信区间的边界。 - 使用
axis.hist()绘制resample_speeds,并用hist_bin_edges指定分箱。 - 使用
axis.axvline,指定percentiles中正确的两个索引,在图上标出置信区间的两条边界线。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create the bootstrap distribution of speeds
resample_speeds = compute_resample_speeds(____, ____)
speed_estimate = np.mean(____)
percentiles = np.percentile(____, [5, 95])
# Plot the histogram with the estimate and confidence interval
fig, axis = plt.subplots()
hist_bin_edges = np.linspace(0.0, 4.0, 21)
axis.hist(____, ____, color='green', alpha=0.35, rwidth=0.8)
axis.axvline(speed_estimate, label='Estimate', color='black')
axis.axvline(percentiles[____], label=' 5th', color='blue')
axis.axvline(percentiles[____], label='95th', color='blue')
axis.legend()
plt.show()