Home > Mobile >  Python - Behavior of `statistics.stdev()` on Numpy arrays
Python - Behavior of `statistics.stdev()` on Numpy arrays

Time:11-07

I have a Numpy array and I would like to use statistics.stdev() to compute its standard deviation. The output of this operation is somewhat unexpected.

import numpy as np
from statistics import stdev, pstdev

>>> a = np.array([0, 1, 2, 3])
>>> stdev(a)
1.0

This is neither the sample standard deviation

>>> stdev([0, 1, 2, 3])
1.2909944487358056

nor the population standard deviation

>>> pstdev([0, 1, 2, 3])
1.118033988749895

I wonder if statistics.stdev() is not supposed to work with Numpy arrays or there is some underlying mechanism causing this behavior?

Note that np.std() works both on lists and Numpy arrays.

>>> np.std([0, 1, 2, 3])
1.118033988749895
>>> np.std(np.array([0, 1, 2, 3]))
1.118033988749895

Both give the population standard deviation as expected.

CodePudding user response:

This happens because the type of numbers, type of numbers in the list is float64 but the type of numbers in numpy.array is int64 you can get what you get with the list with change dtype in NumPy.array to float64 like below:

>>> a = np.array([0, 1, 2, 3])
>>> a.dtype
dtype('int64')
>>> statistics.stdev(a)
1.0

>>> a = np.array([0, 1, 2, 3], dtype=float)
>>> a.dtype
dtype('float64')
>>> statistics.stdev(a)
1.2909944487358056

>>> statistics.stdev([0, 1, 2, 3])
1.2909944487358056
  • Related