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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) numpy(("`NumPy`")) -.-> numpy/MathandStatisticsGroup(["`Math and Statistics`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/multi_array("`Multi-dimensional Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") numpy/MathandStatisticsGroup -.-> numpy/math_ops("`Math Operations`") numpy/MathandStatisticsGroup -.-> numpy/stats("`Statistical Analysis`") subgraph Lab Skills python/comments -.-> lab-86508{{"`NumPy STD Function`"}} python/with_statement -.-> lab-86508{{"`NumPy STD Function`"}} python/variables_data_types -.-> lab-86508{{"`NumPy STD Function`"}} python/numeric_types -.-> lab-86508{{"`NumPy STD Function`"}} python/lists -.-> lab-86508{{"`NumPy STD Function`"}} python/tuples -.-> lab-86508{{"`NumPy STD Function`"}} python/importing_modules -.-> lab-86508{{"`NumPy STD Function`"}} python/numerical_computing -.-> lab-86508{{"`NumPy STD Function`"}} python/build_in_functions -.-> lab-86508{{"`NumPy STD Function`"}} numpy/multi_array -.-> lab-86508{{"`NumPy STD Function`"}} numpy/data_array -.-> lab-86508{{"`NumPy STD Function`"}} numpy/bool_idx -.-> lab-86508{{"`NumPy STD Function`"}} numpy/fancy_idx -.-> lab-86508{{"`NumPy STD Function`"}} numpy/math_ops -.-> lab-86508{{"`NumPy STD Function`"}} numpy/stats -.-> lab-86508{{"`NumPy STD Function`"}} end

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