開始使用免費開始

擲骰子的樣本

來用 numpy 套件產生一個模擬吧。你會延續投影片中的同一個情境,使用 randint() 函式來模擬標準六面骰(1 到 6)的擲骰結果。如果你之前沒用過這個函式,先看看它的文件說明。

從小樣本開始,逐步擴大到大樣本,觀察結果的平均值,並根據觀察對背後的定理解讀並下結論。

本練習屬於課程

用 Python 練習統計面試題

檢視課程

練習說明

  • 使用 randint() 函式產生 10 次擲骰的樣本,指定給變數 small
  • 計算該樣本的平均值指定給 small_mean,並列印結果;注意它與真實平均值有多接近。
  • 以相同方式建立 1000 次擲骰的較大樣本,並將清單指定給變數 large
  • 計算較大樣本的平均值指定給 large_mean,並列印平均值;這裡是在驗證哪一個定理?

動手互動練習

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

from numpy.random import randint

# Create a sample of 10 die rolls
small = randint(____, ____, ____)

# Calculate and print the mean of the sample
small_mean = ____
print(____)

# Create a sample of 1000 die rolls
large = randint(____, ____, ____)

# Calculate and print the mean of the large sample
large_mean = ____
print(____)
編輯並執行程式碼