Scipy.stats.sem 计算标准误差

Scipy.stats.sem calculate of standard error

为什么我得到不同的结果?

from scipy.stats import sem
import numpy as np
l = [0,2,4,5,6,7]
print(sem(l))
print(np.std(l)/np.sqrt(len(l)))

1.0645812948447542

0.9718253158075502

scipy.stats.sem 函数使用默认值 ddof=1 作为自由度数参数,而 numpy.std 默认使用 ddof=0docs:

中也突出显示了这一点

The default value for ddof is different to the default (0) used by other ddof containing routines, such as np.std and np.nanstd.

因此你得到:

>>> print(sem(l))
1.06458129484
>>> print(sem(l, ddof=0))
0.971825315808
>>> print(sem(l, ddof=1))
1.06458129484

>>> print(np.std(l)/np.sqrt(len(l)))
0.971825315808
>>> print(np.std(l, ddof=0)/np.sqrt(len(l)))
0.971825315808
>>> print(np.std(l, ddof=1)/np.sqrt(len(l)))
1.06458129484