开始使用免费开始使用

掷骰子样本

让我们用 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(____)
编辑并运行代码