Python:Numpy标准偏差错误 [英] Python: Numpy standard deviation error

查看:153
本文介绍了Python:Numpy标准偏差错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的测试

import numpy as np
data = np.array([-1,0,1])
print data.std()

>> 0.816496580928

我不知道如何生成此结果?显然:

I don't understand how this result been generated? Obviously:

( (1^0.5 + 1^0.5 + 0^0.5)/(3-1) )^0.5 = 1

在matlab中,它给了我std([-1,0,1]) = 1.您能帮我理解numpy.std()的工作原理吗?

and in matlab it gives me std([-1,0,1]) = 1. Could you help me get understand how numpy.std() works?

推荐答案

此问题的症结在于您需要除以N(3),而不是N-1(2).正如Iarsmans指出的那样,numpy将使用总体方差,而不是样本方差.

The crux of this problem is that you need to divide by N (3), not N-1 (2). As Iarsmans pointed out, numpy will use the population variance, not the sample variance.

所以真正的答案是sqrt(2/3),即:0.8164965...

So the real answer is sqrt(2/3) which is exactly that: 0.8164965...

如果您碰巧试图为自由度使用一个不同的值(而不是默认值0),请使用关键字参数ddof加上一个非0的正值:

If you happen to be trying to deliberately use a different value (than the default of 0) for the degrees of freedom, use the keyword argument ddofwith a positive value other than 0:

np.std(data, ddof=1)

...但是在此处这样做会重新引入您的原始问题,因为numpy将除以N - ddof.

... but doing so here would reintroduce your original problem as numpy will divide by N - ddof.

这篇关于Python:Numpy标准偏差错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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