[Numpy-discussion] average() or mean() errors

Stefan van der Walt stefan at sun.ac.za
Fri Jan 26 11:27:36 CST 2007


On Tue, Jan 23, 2007 at 08:29:47PM -0500, Daniel Smith wrote:
> When calling the average() or mean() functions on a small array (3 
> numbers), I am seeing significant numerical errors (on the order of 1% 
> with data to 8 significant digits). The code I am using is essentially:
> 
> A = zeros(3)
> A[i] = X
> B = average(A)

I'm not sure I understand:

In [7]: A = N.zeros(3)

In [8]: A[1] = 3.

In [9]: N.average(A)
Out[9]: 1.0

In [11]: A[0] = 2.

In [12]: N.average(A)
Out[12]: 1.66666666667

In [13]: (2+3+0)/3.
Out[13]: 1.6666666666666667

In [14]: for i in range(1000):
   ....:     A = N.random.rand(3)
   ....:     assert N.average(A) == N.sum(A)/3.

Maybe you can give a specific code snippet?

Cheers
Stéfan


More information about the Numpy-discussion mailing list