開始使用免費開始

重抽樣結果的視覺化

現在你要把上一個練習的模擬結果視覺化!你會繼續使用 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_weightlowerupper 也已經定義為信賴區間的平均值、2.5% 分位數與 97.5% 分位數。

以下套件已為你載入:randomnumpy 作為 npseaborn 作為 sns,以及 matplotlib.pyplot 作為 plt

本練習屬於課程

Python 的 Monte Carlo 模擬

檢視課程

練習說明

  • 使用 sns.displot() 繪製模擬體重的分佈。
  • 使用 plt.axvline() 繪製 95% 信賴區間的兩條垂直線(依序繪製 lowerupper),顏色為紅色,並以綠色繪製平均值。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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()
編輯並執行程式碼