開始使用免費開始

視覺化 Bootstrap

延續本課先前的內容,來把以 bootstrap 重抽樣估計的速度分佈視覺化。我們對每個樣本都以最小平方法擬合斜率,用來檢驗斜率估計中的變異或不確定性。

為了幫你開始,我們已載入函式 compute_resample_speeds(distances, times),用來計算並產生速度的樣本分佈。

本練習屬於課程

Python 線性建模入門

檢視課程

練習說明

  • 使用預先定義的 compute_resample_speeds(distances, times) 計算 resample_speeds
  • 使用 np.mean()resample_speeds 計算 speed_estimate
  • 使用帶有 [5, 95]np.percentile() 計算 resample_speedspercentiles,作為信賴區間的邊界。
  • 使用 axis.hist() 並以 hist_bin_edges 指定箱數,繪製 resample_speeds
  • 使用 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()
編輯並執行程式碼