View on GitHub

Numpy mean() Weirdness

Using numpy to perform some basic statistics.

Smacked into a major weirdness with the mean() function. I had an array of uint32s; the median() function returns 1370, and the mean returns 418746 ???

I read on Stack Overflow some confusion or problems using mean() with floats, but these are integers!

Some sanity checking:

#arr = np.ndarray((count,), dtype=np.uint32)
len(arr): 10303
np.median(arr): 1370
np.mean(arr): 418746.462487
np.sum(arr): 19377507
np.sum(arr) / len(arr): 1880.76356401

Yup, I'm insane.

So you can get the correct value with

np.sum(arr) / len(arr)

or use:

np.mean(arr3, dtype="uint32", axis=0): 1880

As long as you don't care about the fractional part...