Python: Numpy standard deviation error

前端 未结 3 566
轻奢々
轻奢々 2020-12-29 23:13

This is a simple test

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

>> 0.816496580928

I don\'t understand how this r

3条回答
  •  庸人自扰
    2020-12-29 23:45

    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.

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

    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)
    

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

提交回复
热议问题