手动计算标准差
在视频中,我们讨论了变异性的度量,并提到标准差是最常用的度量。理解这一概念非常重要,因为面试官通常会在流程早期通过编码题或概念性问题来考察它。
在这里,您将通过手动计算标准差来模拟这种体验,也就是说,不使用 std() 之类的现成函数来得到结果。
本练习是课程的一部分
用 Python 练习统计学面试题
练习说明
- 在不使用
mean()函数的情况下,计算已为您定义的nums列表的均值。 - 使用已计算得到的
variance值配合math.sqrt()函数获得标准差;打印结果。 - 使用前面提到的
np.std()函数打印实际的标准差,以检查您的结果。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create a sample list
import math
nums = [1, 2, 3, 4, 5]
# Compute the mean of the list
mean = ____
# Compute the variance and print the std of the list
variance = sum(pow(x - mean, 2) for x in nums) / len(nums)
std = ____
print(____)
# Compute and print the actual result from numpy
real_std = np.array(____).std()
print(____)