開始使用免費開始

平均中的平均

你想知道每筆交易的使用者數(num_users)平均是多少,而且希望用整家公司層級的數字,這樣就能判斷 Amir 的交易使用者數是高於還是低於公司的平均值。問題在於,過去一年公司處理了超過一萬筆交易,要彙整所有資料並不切實際。因此,你將透過對交易進行多次隨機抽樣來估計平均數,因為這比向公司所有人收集資料容易得多。

amir_deals 已可使用,公司的所有交易使用者資料在 all_dealspandas 已以 pd 載入,numpy 也已以 np 載入。

本練習屬於課程

Python 統計學入門

檢視課程

練習說明

  • 將隨機種子設為 321
  • all_deals['num_users'] 以放回抽樣的方式抽取 30 次、每次大小為 20 的樣本,並計算每個樣本的平均數。把這些樣本平均數儲存在 sample_means
  • 列印 sample_means 的平均數。
  • 列印 amir_deals 資料表中 num_users 欄位的平均數。

動手互動練習

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

# Set seed to 321
____

sample_means = []
# Loop 30 times to take 30 means
for i in range(____):
  # Take sample of size 20 from num_users col of all_deals with replacement
  cur_sample = ____
  # Take mean of cur_sample
  cur_mean = ____
  # Append cur_mean to sample_means
  sample_means.append(____)

# Print mean of sample_means
print(____)

# Print mean of num_users in amir_deals
print(____)
編輯並執行程式碼