pandas vs numpy中的不同std [英] Different std in pandas vs numpy

查看:89
本文介绍了 pandas vs numpy中的不同std的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

熊猫和numpy之间的标准差有所不同.为什么以及哪一个是正确的? (相对差异为3.5%,不应四舍五入,在我看来,这是很高的水平.)

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).

示例

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

我使用以下版本:

熊猫: '0.14.0' numpy的: '1.8.1'

pandas: '0.14.0' numpy: '1.8.1'

推荐答案

简而言之,都不是不正确的".熊猫使用无偏估计量(分母中的N-1),而Numpy默认情况下不使用.

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

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

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

有关进一步的讨论,请参见

For further discussion, see

  • Can someone explain biased/unbiased population/sample standard deviation?
  • Population variance and sample variance.
  • Why divide by n-1?

这篇关于 pandas vs numpy中的不同std的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆