開始使用免費開始

放回抽樣

自助抽樣非常適合用來計算平均數的信賴區間;接下來你就要實作看看!

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 的 Monte Carlo 模擬

檢視課程

練習說明

  • 使用 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)
編輯並執行程式碼