NumPy std() function
In this tutorial, we will cover another statistical function named
numpy.std() of the Numpy library.
numpy.std() function "std" stands for standard deviation. Let us first tell you what is standard deviation mean and then we will cover this method.
What is Standard Deviation?
The standard deviation is basically a measure of the amount of variation or dispersion of the set of values.
It is also known as the square root of the average of squared deviations from the mean.
Let us have a look at the formula of dtandard deviation:
std = sqrt(mean(abs(x - x.mean())**2))
To understand how to calculate the mean for array values, you can check this: Numpy numpy.mean() function.
numpy.std() function is used to compute the standard deviation along the specified axis.
This function is used to find the standard deviation that is a measure of the spread of a distribution of the array elements.
By default, the standard deviation is computed for the flattened array, otherwise, it is calculated over the specified axis.
The syntax required to use this function is as follows:
numpy.std(a, axis, dtype, out)
Below we have the description of parameters used by this function:
This parameter is used to indicate the input array.
This parameter is used to indicate the axis along which we want to calculate the median. By default, the input array is flattened(that is working on all the axis). Here, for value of axis, axis = 0 means along the column and axis = 1 means working along the row.
This is an optional parameter that is used to indicate an alternative array in which the output will be stored. The array must have the same dimensions as the expected output.
It is an optional parameter that is used to indicate the type we desire while computing the standard deviation.
This function returns the standard deviation of the array (it will return a scalar value if the axis is none)) or an array with standard deviation values along the specified axis. If
out parameter is None then this function will return a new array containing the standard deviation, otherwise, it will return a reference to the output array.
Below we have a code example where we will use
numpy.std() function to calculate the standard deviation of the 2D Array:
import numpy as np
a = np.array([[11, 2], [13, 44]])
print("The array is:\n",a)
print("Standard Deviation is :")
print("Standard Deviation along axis 0:")
print("Standard Deviation along axis 1:")
The array is:
Standard Deviation is :
Standard Deviation along axis 0:
[ 1. 21.]
Standard Deviation along axis 1:
[ 4.5 15.5]
import numpy as np
inp = [22, 2, 17, 11, 34]
print("The input array is : ")
print("The standard deviation of the Input Array is: ")
print ("\nTo get More precision with float32")
print("Thus std of array is : ", np.std(inp, dtype = np.float32))
print ("\nTo get More accuracy with float64")
print("The std of array is : ", np.std(inp, dtype = np.float64))
The input array is :
[22, 2, 17, 11, 34]
The standard deviation of the Input Array is:
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
float64 is used.
In this tutorial, we covered the
numpy.std() statistical function. We also covered what standard deviation means and its mathematical formula. Then we understood the syntax, parameters of the
numpy.std() function along with the values returned by this method, with some code examples.