问题描述
我有一个列表:
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)
where I calculated its standard deviation in Excel, I got the answer: 0.005106477
, the function I used was: =STDEV(C5:N5)
然后,我使用numpy.std
进行以下计算:
Then I do the same calculation using numpy.std
as:
import numpy as np
print np.std(s)
但是,我得到了答案:0.0048890791894
However, I got the answer: 0.0048890791894
我什至编写了自己的std函数:
I even wrote up my own std function:
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)
和我自己的函数给出的结果与numpy相同.
and my own function gives the same result as numpy.
所以我想知道这样的区别吗?还是只是我犯了一些错误?
So I am wondering is there such a difference? Or it just I made some mistake?
推荐答案
有一个区别:Excel的STDEV
计算样本标准偏差,而NumPy的std
计算人口默认情况下为标准偏差(行为类似于Excel的STDEVP
).
There's a difference: Excel's STDEV
calculates the sample standard deviation, while NumPy's std
calculates the population standard deviation by default (it is behaving like Excel's STDEVP
).
要使NumPy的std
函数的行为类似于Excel的STDEV
,请传入值ddof=1
:
To make NumPy's std
function behave like Excel's STDEV
, pass in the value ddof=1
:
>>> np.std(s, ddof=1)
0.0051064766704396617
这将使用样本方差(即除以n-1
而不是n
)来计算s
的标准偏差.
This calculates the standard deviation of s
using the sample variance (i.e. dividing by n-1
rather than n
.)
这篇关于numpy.std和excel STDEV函数之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!