numpy standard deviation

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

Syntax:

snippet
numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<class numpy._globals._NoValue>)

Parameters

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.

Returns

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.

Example 1:

snippet
a=np.array([[1,4,7,10],[2,5,8,11]])
b=np.std(a)
b

Output:

Output
3.391164991562634

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'a' via array() function.
  • We have declared the variable 'b' and assigned the returned value of std() function.
  • We have passed the array 'a' in the function
  • Lastly, we tried to print the value of 'b'.

In the output, an array containing standard deviation has been shown.

Example 2:

snippet
a=np.array([[1,4,7,10],[2,5,8,11]])
b=np.std(a, axis=0)
b

Output:

Output
array([0.5, 0.5, 0.5, 0.5])

Example 3:

snippet
a=np.array([[1,4,7,10],[2,5,8,11]])
b=np.std(a, axis=1)
b

Output:

Output
array([3.35410197, 3.35410197])

Example 4:

snippet
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

  • We have imported numpy with alias name np.
  • We have created an array 'a' using np.zeros() function with data type np.float32.
  • We have assigned the value 0.1 to the elements of the 1st row and 1.0 to the elements of the second row.
  • We have passed the array 'a' in the function
  • Lastly, we tried to print the value of 'b'.

In the output, the standard deviation has been shown, which can be inaccurate.

Output:

Output
0.45000008

Example 5:

snippet
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:

Output
0.4499999992549418
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +