How to Calculate Standard Deviation with NumPy in Python
Use
numpy.std() to calculate the standard deviation of an array. Pass your data array to numpy.std(), and it returns the spread of values around the mean as a single number.Syntax
The basic syntax to calculate standard deviation with NumPy is:
numpy.std(a, axis=None, ddof=0, keepdims=False)
Where:
a: Input array or data.axis: Axis along which to compute the std. Default isNone(compute over all data).ddof: Delta degrees of freedom. The divisor used in calculation isN - ddof. Default is 0 for population std.keepdims: IfTrue, keeps reduced dimensions for broadcasting.
python
numpy.std(a, axis=None, ddof=0, keepdims=False)
Example
This example shows how to calculate the standard deviation of a list of numbers using NumPy.
python
import numpy as np data = np.array([10, 12, 23, 23, 16, 23, 21, 16]) std_dev = np.std(data) print(f"Standard Deviation: {std_dev:.2f}")
Output
Standard Deviation: 5.14
Common Pitfalls
One common mistake is confusing population standard deviation with sample standard deviation. By default, numpy.std() calculates population std (dividing by N). For sample std, set ddof=1 to divide by N-1.
Another pitfall is forgetting to specify the axis when working with multi-dimensional arrays, which can lead to unexpected results.
python
import numpy as np data = np.array([10, 12, 23, 23, 16, 23, 21, 16]) # Wrong: default is population std pop_std = np.std(data) # Correct: sample std with ddof=1 sample_std = np.std(data, ddof=1) print(f"Population std: {pop_std:.2f}") print(f"Sample std: {sample_std:.2f}")
Output
Population std: 5.14
Sample std: 5.44
Quick Reference
| Parameter | Description | Default |
|---|---|---|
| a | Input array or data | Required |
| axis | Axis to compute std along | None (all data) |
| ddof | Delta degrees of freedom (N - ddof divisor) | 0 (population std) |
| keepdims | Keep reduced dimensions | False |
Key Takeaways
Use numpy.std() to calculate standard deviation of data arrays easily.
Set ddof=1 to get sample standard deviation instead of population standard deviation.
Specify axis parameter when working with multi-dimensional arrays to control calculation direction.
By default, numpy.std() calculates population standard deviation dividing by N.
Always check if you need sample or population std to avoid incorrect results.