How to Calculate Standard Deviation in NumPy (np.std with ddof, axis, and Population/Sample Examples)

This guide shows how to calculate standard deviation in NumPy using np.std(), supporting both population standard deviation (default ddof=0) and sample standard deviation (ddof=1).

standard deviation numpy

Standard deviation is a statistical measure that quantifies the dispersion or spread of a dataset around its mean (average) value. A low standard deviation indicates that the data points tend to be close to the mean, while a high standard deviation signifies that the data points are spread out over a wider range.

See also  How to add two arrays in Numpy?

Standard Deviation in NumPy

NumPy’s np.std() function makes it easy to calculate standard deviation in NumPy arrays along specific axes or for the entire dataset, with full control over ddof for statistical accuracy.

Numpy provides a dedicated function, std, for calculating standard deviation.

import numpy as np

my_array = np.array([1, 5, 7, 5, 43, 43, 8, 43, 6])

standard_deviation = np.std(my_array)
print("Standard deviation equals: " + str(round(standard_deviation, 2)))

In its basic form, np.std(my_array) calculates the population standard deviation. NumPy’s std function offers several optional parameters to customize the calculation further. For example, the axis parameter allows you to compute the standard deviation along a specific axis in a multi-dimensional array (e.g., axis=0 for columns, axis=1 for rows). The ddof (Delta Degrees of Freedom) parameter allows you to control the divisor used in the calculation; by default, ddof=0 for population standard deviation, but you can set ddof=1 to calculate the sample standard deviation (which is more common in statistical inference).

See also  How to calculate definite integral in Python

Standard deviation is widely used in data analysis to understand the spread of data, identify outliers, and compare the variability of different datasets. In finance, it’s a key measure of risk or volatility of investments. In engineering and science, it’s used to assess the precision and reliability of measurements and experiments.

See also  How to rank values in Numpy array?