開始使用免費開始

排列(Permutation)練習

NBA 球員是否比美國成年男性更重?你現在要計算 NBA 球員與美國成年男性之間「平均體重差」(單位:公斤)的 95% 信賴區間。你會使用提供的兩個清單。

在檢定差異時,排列檢定很實用,所以這裡你將使用這種重抽樣方法!

nba_weights = [96.7, 101.1, 97.9, 98.1, 98.1, 100.3, 101.0, 98.0, 97.4, 100.5, 100.3, 100.2, 100.6]
us_adult_weights = [75.1, 100.1, 95.2, 81.0, 72.0, 63.5, 80.0, 97.1, 94.3, 80.3, 93.5, 85.8, 95.1]

請注意,上述每個清單都各有 13 個體重數值。

以下物件已為你匯入:random、將 numpy 匯入為 np、將 seaborn 匯入為 sns,以及將 matplotlib.pyplot 匯入為 plt

本練習屬於課程

Python 的 Monte Carlo 模擬

檢視課程

練習說明

  • 定義 all_weights 為一個清單,包含 nba_weightsus_adult_weights 的所有數值。
  • 使用 np.random.permutation()all_weights 進行排列。
  • 將前 13 個重新排列後的樣本指定給 perm_nba,其餘 13 個指定給 perm_adult

動手互動練習

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

# Define all_weights
all_weights = ____
simu_diff = []

for i in range(1000):
	# Perform the permutation on all_weights
    perm_sample = ____
    # Assign the permutated samples to perm_nba and perm_adult
    perm_nba, perm_adult = ____, ____
    perm_diff = np.mean(perm_nba) - np.mean(perm_adult)
    simu_diff.append(perm_diff)
mean_diff = np.mean(nba_weights) - np.mean(us_adult_weights) 
upper = np.quantile(simu_diff, 0.975)
lower = np.quantile(simu_diff, 0.025)
print(mean_diff, lower, upper)
編輯並執行程式碼