numpy.std和excel STDEV函数有什么区别吗?
我有一个列表:
s = [0.995537725, 0.994532199, 0.996027983, 0.999891383, 1.004754272, 1.003870012, 0.999888944, 0.994438078, 0.992548715, 0.998344545, 1.004504764, 1.00883411]
我在Excel中计算了它的标准偏差,我得到了答案: 0.005106477
,我使用的函数是: =STDEV(C5:N5)
然后我使用numpy.std
进行相同的计算:
import numpy as np print np.std(s)
不过,我得到了答案: 0.0048890791894
我甚至写了我自己的std函数:
def std(input_list): count = len(input_list) mean = float(sum(input_list)) / float(count) overall = 0.0 for i in input_list: overall = overall + (i - mean) * (i - mean) return math.sqrt(overall / count)
和我自己的function给与numpy相同的结果。
所以我想知道有这样的区别吗? 还是只是我犯了一些错误?
有一个区别:Excel的STDEV
计算样本标准偏差,而NumPy的std
默认计算总体标准偏差(其行为与Excel的STDEVP
相似)。
要使NumPy的std
函数像Excel的STDEV
一样运行,请将值ddof=1
:
>>> np.std(s, ddof=1) 0.0051064766704396617
这使用样本方差(即除以n-1
而不是n
)来计算s
的标准偏差。