NumPy STD Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will cover the numpy.std() function of the NumPy library. We will understand what standard deviation means and how to use numpy.std() to calculate the standard deviation of an array.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Understanding Standard Deviation

Standard deviation is a measure of the amount of variation or dispersion of the set of values. Mathematically, the standard deviation is defined as the square root of the average of squared deviations from the mean. Let us have a look at the formula of standard deviation:

std = \sqrt{\frac{\sum_{i=1}^{n} (x_i-\bar{x})^2}{n}}

Here, \bar{x} is the mean of the array elements, x_i is the i-th element of the array, and n is the number of elements in the array.

Syntax of numpy.std()

The syntax required to use the numpy.std() function is as follows:

numpy.std(a, axis=None, dtype=None, out=None)

Parameters:

  • a: Input array
  • axis: Axis along which to calculate standard deviation. By default, it is calculated on a flattened array.
  • dtype: Desired data type of returned output
  • out: Output array, in which the output is stored.

Returns:

Returns the standard deviation of the array or an array with standard deviation values along the specified axis.

Example

Let's look at a simple example using numpy.std().

import numpy as np

## create 2D array
a = np.array([[11, 2], [13, 44]])
print("The array is:\n",a)

## calculate standard deviation of flattened array
print("Standard Deviation is :")
print(np.std(a))

## calculate standard deviation along axis 0
print("Standard Deviation along axis 0:")
print(np.std(a, axis=0))

## calculate standard deviation along axis 1
print("Standard Deviation along axis 1:")
print(np.std(a, axis=1))

Output:

The array is:
[[11  2]
 [13 44]]
Standard Deviation is :
15.850867484147358
Standard Deviation along axis 0:
[ 1. 21.]
Standard Deviation along axis 1:
[ 4.5 15.5]

Precision

Let's look at an example where we can specify data type for the output.

import numpy as np

inp = [22, 2, 17, 11, 34]

print("The input array is : ")
print(inp)

## calculate standard deviation
print("The standard deviation of the Input Array is: ")
print(np.std(inp))

## get more precision with float 32
print("\nTo get More precision with float32")
print("Thus std of array is : ", np.std(inp, dtype=np.float32))

## get more accuracy with float 64
print("\nTo get More accuracy with float64")
print("The std of array is : ", np.std(inp, dtype=np.float64))

Output:

The input array is:
[22, 2, 17, 11, 34]
The standard deviation of the Input Array is:
10.721940122944167

To get More precision with float32
Thus std of array is : 10.72194

To get More accuracy with float64
The std of array is: 10.721940122944167

Note: In order to compute the standard deviation more accurately, dtype float64 is used.

Summary

In this lab, we learned about the numpy.std() function, which is used to calculate the standard deviation of an array along the specified axis. Additionally, we understood the syntax of numpy.std() and the different parameters that we can pass. Finally, we saw some examples to understand how numpy.std() works.

Other Python Tutorials you may like