pandas 大熊猫与 numpy 中的不同标准

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24984178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:17:54  来源:igfitidea点击:

Different std in pandas vs numpy

pythonnumpypandasprecision

提问by Mannaggia

The standard deviation differs between pandas and numpy. Why and which one is the correct one? (the relative difference is 3.5% which should not come from rounding, this is high in my opinion).

pandas 和 numpy 的标准偏差不同。为什么,哪一个是正确的?(相对差异是 3.5%,这不应该来自四舍五入,我认为这是很高的)。

Example

例子

import numpy as np
import pandas as pd
from StringIO import StringIO

a='''0.057411
0.024367
 0.021247
-0.001809
-0.010874
-0.035845
0.001663
0.043282
0.004433
-0.007242
0.029294
0.023699
0.049654
0.034422
-0.005380'''


df = pd.read_csv(StringIO(a.strip()), delim_whitespace=True, header=None)

df.std()==np.std(df) # False
df.std() # 0.025801
np.std(df) # 0.024926

(0.024926 - 0.025801) / 0.024926 # 3.5% relative difference

I use these versions:

我使用这些版本:

pandas: '0.14.0' numpy: '1.8.1'

Pandas:'0.14.0' numpy:'1.8.1'

回答by NPE

In a nutshell, neither is "incorrect". Pandas uses the unbiased estimator(N-1in the denominator), whereas Numpy by default does not.

简而言之,两者都不是“不正确的”。Pandas 使用无偏估计器N-1在分母中),而 Numpy 默认不使用。

To make them behave the same, pass ddof=1to numpy.std().

要使它们的行为相同,请传递ddof=1numpy.std().

For further discussion, see

有关进一步讨论,请参见

回答by Xuan

For pandasto performed the same as numpy, you can pass in the ddof=0parameter, so df.std(ddof=0).

对于pandasto 执行与 相同numpy,您可以传入ddof=0参数,因此df.std(ddof=0)

This short video explains quite well why n-1might be preferred for samples. https://www.youtube.com/watch?v=Cn0skMJ2F3c

这个简短的视频很好地解释了为什么n-1可能更喜欢样品。https://www.youtube.com/watch?v=Cn0skMJ2F3c