2

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.