开始使用免费开始使用

检查结果

现在请您自己生成一些样本并计算期望与方差,然后使用 binom 提供的方法检查样本值是否与理论值一致。

scipy.stats 中的 binom 对象和 describe() 方法已加载,您可以直接进行计算。

本练习是课程的一部分

Python 概率基础

查看课程

练习说明

  • 计算变量 sample 的样本均值和方差。
  • 使用 n=10p=0.3 计算期望,并使用 mean1 - p 计算方差。
  • 使用 binom 的方法获取在 p=0.3 下抛 10 次硬币的期望和方差。

交互式实操练习

通过完成这段示例代码来试试这个练习。

sample = binom.rvs(n=10, p=0.3, size=2000)

# Calculate the sample mean and variance from the sample variable
sample_describe = describe(____)

# Calculate the sample mean using the values of n and p
mean = ____*____

# Calculate the sample variance using the value of 1-p
variance = mean*____

# Calculate the sample mean and variance for 10 coin flips with p=0.3
binom_stats = binom.stats(n=____, p=____)

print(sample_describe.mean, sample_describe.variance, mean, variance, binom_stats)
编辑并运行代码