When working with data in Python, you’ll often need to measure how spread out the values are. A common way to do this is by calculating the standard deviation.
With NumPy, it’s straightforward:
Population standard deviation:
np.std(data, ddof=0)Sample standard deviation:
np.std(data, ddof=1)
import numpy as np
# Calculate the sample standard deviation
sample_std = round(np.std(observations, ddof=1), 2)
# Calculate the population standard deviation
population_std = round(np.std(observations, ddof=0), 2)
sample_std, population_std
Here, ddof stands for Delta Degrees of Freedom:
- ddof=0 is used for the whole population.
- ddof=1 is used for a sample (this is the unbiased estimator).
Result:
For this dataset, the sample standard deviation is 1.55, and the population standard deviation is 1.45.