Numpy.std – Python NumPy std() Function

NumPy std() Function:

Numpy.std: The standard deviation along the given axis is calculated using the std() function of the NumPy module.

The square root of the average of the squared deviations from the mean is the standard deviation. It can be expressed mathematically as:

std = sqrt(mean(abs(x – x.mean())**2))

Syntax:

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

Parameters

a: This is required. It is an array given as input.

axis: This is optional. It indicates the axis or axes along which the standard deviation is computed. The default, axis=None, calculates the flattened array’s standard deviation.

dtype: This is optional. It indicates the type that will be used to calculate the standard deviation. The default for integer arrays is float64, but the default for float arrays is the same as the array type.

out: It indicates the output array in which the result will be stored. It must be the same shape as the desired output.

keepdims: This is optional. The reduced axes are left in the output as dimensions with size one if this is set to True. The result will broadcast correctly against the input array if you use this option.

Return Value:

Std in python: If out=None, returns an array with the standard deviation; otherwise, returns a reference to the output array.

NumPy std() Function in Python

Example1

Numpy std: The standard deviation of all values in the array is calculated using the std() method.

Approach:

  • Import numpy module using the import keyword
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above given array.
  • Pass the given array as an argument to the std() function of numpy module to calculate the standard deviation of all values in the given array.
  • Store it in another variable.
  • Print the standard deviation of all values in the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[5,25],[35, 40]])              
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array as an argument to the std() function of numpy module 
# to calculate the standard deviation of all values in the given array 
# Store it in another variable.
rslt = np.std(gvn_arry)
# Print the standard deviation of all values in the given array 
print("The standard deviation of all values in the given array =", rslt)

Output:

The above given array is:
[[ 5 25]
 [35 40]]
The standard deviation of all values in the given array = 13.40475661845451

Example2

NP standard deviation: Standard deviation is calculated over the specified axes when the axis parameter is defined.

Approach:

  • Import numpy module using the import keyword
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above given array.
  • Pass the given array, axis=0 as the arguments to the std() function of numpy module to calculate the standard deviation of all values in the given array along axis=0.
  • Store it in another variable.
  • Print the standard deviation of all values the given array along axis=0
  • Pass the given array, axis=0 as the arguments to the std() function of numpy module to calculate the standard deviation of all values in the given array along axis=1.
  • Store it in another variable.
  • Print the standard deviation of all values the given array along axis=1
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[5, 25, 50],[35, 40, 60]])              
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array, axis=0 as the arguments to the std() function of numpy module 
# to calculate the standard deviation of all values in the given array along axis=0
# Store it in another variable.
rslt_1 = np.std(gvn_arry, axis=0)
# Print the standard deviation of all values the given array along axis=0
print("The standard deviation of all values in the given array along axis=0: ", rslt_1)
# Pass the given array, axis=1 as the arguments to the std() function of numpy module 
# to calculate the standard deviation of all values in the given array along axis=1
# Store it in another variable.
rslt_2 = np.std(gvn_arry, axis=1)
# Print the standard deviation of all values the given array along axis=1
print("The standard deviation of all values in the given array along axis=1: ", rslt_2)

Output:

The above given array is:
[[ 5 25 50]
[35 40 60]]
The standard deviation of all values in the given array along axis=0: [15. 7.5 5. ]
The standard deviation of all values in the given array along axis=1: [18.40893503 10.8012345 ]

Example3

NP std python: The standard deviation can be calculated in float64 for a more accurate result.

Approach:

  • Import numpy module using the import keyword
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Pass the given array, datatype float32 as the arguments to the std() function of numpy module to calculate the standard deviation of all values in the given array with float32 and print it.
  • Pass the given array, datatype float64 as the arguments to the std() function of numpy module to calculate the standard deviation of all values in the given array with float64 and print it.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([10, 5, 55, 200])              
# Pass the given array, datatype float32 as the arguments to the std() function of numpy module 
# to calculate the standard deviation of all values in the given array with float32 and print it
print("The standard deviation of all values in the given array with float32:")
print(np.std(gvn_arry, dtype=np.float32))
# Pass the given array, datatype float64 as the arguments to the std() function of numpy module 
# to calculate the standard deviation of all values in the given array with float64 and print it
print("The standard deviation of all values in the given array with float64:")
print(np.std(gvn_arry, dtype=np.float64))

Output:

The standard deviation of all values in the given array with float32:
78.93827
The standard deviation of all values in the given array with float64:
78.93826701923471