In 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))
Numpy numpy.std()
function:
The 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.
Syntax of numpy.std()
:
The syntax required to use this function is as follows:
numpy.std(a, axis, dtype, out)
Parameters:
Below we have the description of parameters used by this function:
- a
This parameter is used to indicate the input array. - axis
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. - out
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. - dtype
It is an optional parameter that is used to indicate the type we desire while computing the standard deviation.
Returned Values:
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.
Example 1:
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(np.std(a))
print("Standard Deviation along axis 0:")
print(np.std(a, axis=0))
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]
Example 2:
import numpy as np
inp = [22, 2, 17, 11, 34]
print("The input array is : ")
print(inp)
print("The standard deviation of the Input Array is: ")
print(np.std(inp))
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))
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.