Open In App

numpy.std() in Python

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

numpy.std() is a function provided by the NumPy library that calculates the standard deviation of an array or a set of values. Standard deviation is a measure of the amount of variation or dispersion of a set of values.

[Tex]\text{Standard Deviation} = \sqrt{\text{mean} \left( (x – x.\text{mean}())^2 \right)}[/Tex]

For example:

x = 1 1 1 1 1
Standard Deviation = 0 .

y = 9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4
Step 1 : Mean of distribution 4 = 7
Step 2 : Summation of (x – x.mean())**2 = 178
Step 3 : Finding Mean = 178 /20 = 8.9
This Result is Variance.
Step 4 : Standard Deviation = sqrt(Variance) = sqrt(8.9) = 2.983..

Syntax

numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False)

Parameters:

  • a: array-like or sequence of numbers. This is the input array or list of values for which the standard deviation is to be computed.
  • axis: {None, int, tuple of int}, optional. This specifies the axis along which the standard deviation is calculated. If None, the standard deviation is computed over the entire array.
  • dtype: data-type, optional. The data type used in the computation. By default, it is the data type of the input array.
  • out: ndarray, optional. A location to store the result. If provided, the result will be placed in this array.
  • ddof: int, optional. “Delta Degrees of Freedom”. The divisor used in the calculation is N – ddof, where N is the number of elements. The default is ddof=0, which gives the population standard deviation.
  • keepdims: bool, optional. If True, the reduced axes are retained in the result as dimensions with size one. This can be useful for broadcasting.

Return Value: The standard deviation of the elements in the array, computed along the specified axis or over the entire array.

Examples of numpy.std()

Example 1: Standard Deviation of 1D Array with numpy.std()

This example demonstrates how to calculate the standard deviation of a 1D array using numpy.std().

Python
import numpy as np 
	
arr = [20, 2, 7, 1, 34] 

print("arr : ", arr) 
print("std of arr : ", np.std(arr)) 

print ("\nMore precision with float32") 
print("std of arr : ", np.std(arr, dtype = np.float32)) 

print ("\nMore accuracy with float64") 
print("std of arr : ", np.std(arr, dtype = np.float64)) 

Output
arr :  [20, 2, 7, 1, 34]
std of arr :  12.576167937809991

More precision with float32
std of arr :  12.576168

More accuracy with float64
std of arr :  12.576167937809991

Explanation: This code calculates the standard deviation of a 1D array, first using the default data type. Then, it shows how to increase precision by specifying dtype=np.float32 and dtype=np.float64. This method is useful for managing the precision and accuracy of numerical calculations.

Example 2: Standard Deviation of 2D Array with numpy.std()

This example demonstrates how to compute the standard deviation of a 2D array using numpy.std().

Python
import numpy as np 
 
arr = [[2, 2, 2, 2, 2], 
	[15, 6, 27, 8, 2], 
	[23, 2, 54, 1, 2, ], 
	[11, 44, 34, 7, 2]] 
 
print("\nstd of arr, axis = None : ", np.std(arr)) 
print("\nstd of arr, axis = 0 : ", np.std(arr, axis = 0)) 
print("\nstd of arr, axis = 1 : ", np.std(arr, axis = 1)) 

Output

std of arr, axis = None : 15.3668474320532

std of arr, axis = 0 : [ 7.56224173 17.68473918 18.592 67329 3.04138127 0. ]

std of arr, axis = 1 : [ 0. 8.7772433 20.53874388 16.40243884]

Explanation: This code calculates the standard deviation for a 2D array in three ways: across the entire array (flattened), along axis=0 (columns), and along axis=1 (rows). It illustrates how numpy.std() can be used to calculate standard deviation along different dimensions of an array.



Next Article

Similar Reads