Python

NumPy np.std()

The std() function in NumPy is used to calculate the Standard Deviation of array elements along a given axis.

Before diving into using the std() function in NumPy, let us recap what standard deviation is.

What is Standard Deviation?

Standard Deviation or SD is a typical statistical operation that allows you to calculate the dispersion of a given set of values.

We can express the formula for standard deviation as follows:

With that off the way, let us discuss how to use the NumPy std() function.

NumPy std Function

The std() function calculates the standard deviation of elements in an array along a given axis.

If the axis is not specified, the function will flatten the array and return the standard deviation of all the elements.

The function syntax can be expressed in the following:

numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>, *, where=<no value>)

The parameters are defined according to their following functions:

  1. a – specifies the input array.
  2. axis – defines the axis along which to calculate the standard deviation of the elements. Check the NumPy axis documentation to discover more.
  3. dtype – defines the data type of the output.
  4. out – specifies an alternative array in which to store the result. The alternative array must be of the same shape as the expected output.
  5. ddof – establishes the Delta Degrees of Freedom value. DDOF refers to a divisor used to calculate the number of elements.

Example 1

The following code shows an example of the NumPy std function without an axis value:

# import numpy
import numpy as np
# create array
arr = np.array([[1, 2], [3, 4]])
# return std value
print(f"Standard Deviation: {np.std(arr)}")

The previous code returns the standard deviation of all elements in the array.

The resulting output is as follows:

Standard Deviation: 1.118033988749895

Example 2

To calculate the standard deviation along axis 0 and axis 1, apply the following code:

print(f"Standard Deviation (axis=0): {np.std(arr, axis=0)}")
print(f"Standard Deviation (axis=1): {np.std(arr, axis=1)}")

The following is the resulting output:

Standard Deviation (axis=0): [1. 1.]
Standard Deviation (axis=1): [0.5 0.5]

Example 3

You can specify a data type such as float to increase accuracy and precision. An example code is as follows:

print(f"Standard Deviation: {np.std(arr, dtype=np.float32)}")
print(f"Standard Deviation: {np.std(arr, dtype=np.float64)}")

You will notice that np.float32 returns a value with a higher precision while np.float64 returns a value with a higher accuracy.

The following is the resulting output:

Standard Deviation: 1.1180340051651
Standard Deviation: 1.118033988749895

Example 4

Similarly, you can use the std() function with an N-dimensional array as shown below:

arr = [[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],
       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],
       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]]
print(f"Standard Deviation: {np.std(arr)}")

The given example calculates the standard deviation of a 3D array and returns the result as follows:

Standard Deviation: 7.788880963698615

NOTE: Since we do not specify the axis, the function flattens the array and returns the resulting Standard Deviation value.

Conclusion

In this article, we explored how to use the NumPy std() function to calculate the standard deviation of an array along a specified axis following the given examples. Browse through Linux Hint website for more related articles.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list