Python:NumPy .std()
The NumPy .std() function calculates the NumPy standard deviation of given data along a specified axis. Standard deviation is a statistical measure that indicates how spread out the values in a dataset are, represented by an array, along a specified axis.
NumPy .std() Syntax
np.std(a, axis, dtype, out, ddof, keepdims, where)
Parameters:
a: Array of elements used to find the standard deviation.axis(Optional): The axis along which the standard deviation will be computed. By default, the array is flattened before computation.- If
0, calculates the standard deviation along the vertical axis. - If
1, calculates the standard deviation along the horizontal axis. - If a tuple of integers, calculates the standard deviation along multiple specified axes.
- If
dtype(Optional): Type used in computing the standard deviation, if specified. By default, for arrays of integer type, it is float64, while for arrays of float types, it matches the array type.out(Optional): Specifies an alternative output array to contain the result. This array must have the same shape as the expected output.ddof(Optional): It stands for Delta Degrees of Freedom. It helps adjust the calculation of standard deviation for samples.keepdims(Optional): It accepts a boolean value and is used to determine whether to retain the dimensions of the given array in the output. By default, it is set toFalse.where(Optional): It accepts boolean arrays or conditions whereTruevalues indicate the indices, or elements within the array for which the standard deviation should be calculated.
Return value:
If the out parameter is None, the NumPy .std() function returns a new array containing the standard deviation. Otherwise, it assigns the result to the specified output array and returns its reference.
Notes:
- For floating-point inputs, the standard deviation is calculated with the same precision as the input data. This may cause inaccuracies, especially with
np.float32data type.- For complex numbers,
stdtakes the absolute value before squaring for a real, non-negative result.
Example 1: Basic Usage of NumPy .std()
In this example, the NumPy .std() function calculates the standard deviation of the given data:
import numpy as npdata = [10, 13, 23, 23, 16, 23, 21, 16]std_dev = np.std(data)print("Standard Deviation:", std_dev)
The output of this code is:
Standard Deviation: 4.7549316504025585
Example 2: Using NumPy .std() with axis
In this example, the axis parameter is used with the NumPy .std() function to calculate the standard deviation of the given data:
import numpy as nparr = np.array([[1, 2, 3],[4, 5, 6]])col_std = np.std(arr, axis=0)row_std = np.std(arr, axis=1)print("Column-wise STD:", col_std)print("Row-wise STD:", row_std)
The output of this code is:
Column-wise STD: [1.5 1.5 1.5]Row-wise STD: [0.81649658 0.81649658]
Codebyte Example: Using NumPy .std() with ddof
In this codebyte example, the ddof parameter is used with the NumPy .std() function to calculate the standard deviation of the given data:
Frequently Asked Questions
1. What is the difference between NumPy .std() and stdev()?
NumPy .std() works on NumPy arrays and is optimized for performance, supporting multi-dimensional data and the axis parameter. Python’s built-in statistics.stdev() works on standard Python iterables, calculates the sample standard deviation by default, and doesn’t support multi-dimensional arrays or the axis argument.
2. Is NumPy .std() population or sample?
By default, NumPy .std() calculates the population standard deviation (ddof=0). To compute the sample standard deviation, set ddof=1.
3. What is the difference between NumPy .std() and Pandas .std()?
NumPy .std() calculates standard deviation on NumPy arrays and defaults to population standard deviation (ddof=0). Pandas .std() works on Series and DataFrame objects, automatically excludes NaN values, and defaults to the sample standard deviation (ddof=1).
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours