The numpy module of Python provides a function called numpy.std(), used to compute the standard deviation along the specified axis. This function returns the standard deviation of the array elements. The square root of the average square deviation (computed from the mean), is known as the standard deviation. By default, the standard deviation is calculated for the flattened array. With the help of the x.sum()/N, the average square deviation is normally calculated, and here, N=len(x).
Standard Deviation=sqrt(mean(abs(x-x.mean( ))**2
numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<class numpy._globals._NoValue>)
a: array_like
This parameter defines the source array whose elements standard deviation is calculated.
axis: None, int, or tuple of ints(optional)
It is the axis along which the standard deviation is calculated. The standard deviation of the flattened array is computed by default. If it is a tuple of ints, performs standard deviation over multiple axis instead of a single axis or all axis as before.
dtype : data_type(optional)
This parameter defines the data type, which is used in computing the standard deviation. By default, the data type is float64 for integer type arrays, and, for float types array, it will be the same as the array type.
out : ndarray(optional)
This parameter defines the alternative output array in which the result is to be placed. This alternative ndarray has the same shape as the expected output. But we cast the type when necessary.
ddof : int(optional)
This parameter defines the Delta Degrees of Freedom. The N-ddof divisor is used in calculations, where N is the number of elements. By default, the value of this parameter is set to 0.
keepdims : bool(optional)
It is optional, whose value, when true, will leave the reduced axis as dimensions with size one in the resultant. When it passes the default value, it will allow the non-default values to pass via the mean method of sub-classes of ndarray, but the keepdims will not pass. Also, the output or the result will broadcast against the input array correctly.
This function will return a new array that contains the standard deviation. If we do not set the 'out' parameter to None, it returns the output array's reference.
a=np.array([[1,4,7,10],[2,5,8,11]]) b=np.std(a) b
Output:
In the above code
In the output, an array containing standard deviation has been shown.
a=np.array([[1,4,7,10],[2,5,8,11]]) b=np.std(a, axis=0) b
Output:
a=np.array([[1,4,7,10],[2,5,8,11]]) b=np.std(a, axis=1) b
Output:
import numpy as np a = np.zeros((2, 512*512), dtype=np.float32) a[1, :] = 1.0 a[0, :] = 0.1 b=np.std(a) b
In the above code
In the output, the standard deviation has been shown, which can be inaccurate.
Output:
import numpy as np a = np.zeros((2, 512*512), dtype=np.float32) a[1, :] = 1.0 a[0, :] = 0.1 b=np.std(a ,dtype=np.float64)) b
Output: