numpy.std() is a function provided by the NumPy library that calculates the standard deviation of an array or a set of values. Standard deviation is a measure of the amount of variation or dispersion of a set of values.
[Tex]\text{Standard Deviation} = \sqrt{\text{mean} \left( (x – x.\text{mean}())^2 \right)}[/Tex]
For example:
x = 1 1 1 1 1
Standard Deviation = 0 .
y = 9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4
Step 1 : Mean of distribution 4 = 7
Step 2 : Summation of (x – x.mean())**2 = 178
Step 3 : Finding Mean = 178 /20 = 8.9
This Result is Variance.
Step 4 : Standard Deviation = sqrt(Variance) = sqrt(8.9) = 2.983..
Syntax
numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False)
Parameters:
- a: array-like or sequence of numbers. This is the input array or list of values for which the standard deviation is to be computed.
- axis: {None, int, tuple of int}, optional. This specifies the axis along which the standard deviation is calculated. If None, the standard deviation is computed over the entire array.
- dtype: data-type, optional. The data type used in the computation. By default, it is the data type of the input array.
- out: ndarray, optional. A location to store the result. If provided, the result will be placed in this array.
- ddof: int, optional. “Delta Degrees of Freedom”. The divisor used in the calculation is N – ddof, where N is the number of elements. The default is ddof=0, which gives the population standard deviation.
- keepdims: bool, optional. If True, the reduced axes are retained in the result as dimensions with size one. This can be useful for broadcasting.
Return Value: The standard deviation of the elements in the array, computed along the specified axis or over the entire array.
Examples of numpy.std()
Example 1: Standard Deviation of 1D Array with numpy.std()
This example demonstrates how to calculate the standard deviation of a 1D array using numpy.std().
Python
import numpy as np
arr = [20, 2, 7, 1, 34]
print("arr : ", arr)
print("std of arr : ", np.std(arr))
print ("\nMore precision with float32")
print("std of arr : ", np.std(arr, dtype = np.float32))
print ("\nMore accuracy with float64")
print("std of arr : ", np.std(arr, dtype = np.float64))
Outputarr : [20, 2, 7, 1, 34]
std of arr : 12.576167937809991
More precision with float32
std of arr : 12.576168
More accuracy with float64
std of arr : 12.576167937809991
Explanation: This code calculates the standard deviation of a 1D array, first using the default data type. Then, it shows how to increase precision by specifying dtype=np.float32 and dtype=np.float64. This method is useful for managing the precision and accuracy of numerical calculations.
Example 2: Standard Deviation of 2D Array with numpy.std()
This example demonstrates how to compute the standard deviation of a 2D array using numpy.std().
Python
import numpy as np
arr = [[2, 2, 2, 2, 2],
[15, 6, 27, 8, 2],
[23, 2, 54, 1, 2, ],
[11, 44, 34, 7, 2]]
print("\nstd of arr, axis = None : ", np.std(arr))
print("\nstd of arr, axis = 0 : ", np.std(arr, axis = 0))
print("\nstd of arr, axis = 1 : ", np.std(arr, axis = 1))
Output
std of arr, axis = None : 15.3668474320532
std of arr, axis = 0 : [ 7.56224173 17.68473918 18.592 67329 3.04138127 0. ]
std of arr, axis = 1 : [ 0. 8.7772433 20.53874388 16.40243884]
Explanation: This code calculates the standard deviation for a 2D array in three ways: across the entire array (flattened), along axis=0 (columns), and along axis=1 (rows). It illustrates how numpy.std() can be used to calculate standard deviation along different dimensions of an array.
Similar Reads
numpy.stack() in Python
NumPy is a famous Python library used for working with arrays. One of the important functions of this library is stack(). Important points:stack() is used for joining multiple NumPy arrays. Unlike, concatenate(), it joins arrays along a new axis. It returns a NumPy array.to join 2 arrays, they must
5 min read
numpy.sum() in Python
This function returns the sum of array elements over the specified axis. Syntax: numpy.sum(arr, axis, dtype, out): Parameters: arr: Input array. axis: The axis along which we want to calculate the sum value. Otherwise, it will consider arr to be flattened(works on all the axes). axis = 0 means along
3 min read
Python | Numpy matrix.std()
With the help of matrix.std() method, we are able to find the standard deviation a matrix by using the same method. Syntax : matrix.std() Return : Return standard deviation of a matrix Example #1 : In this example we are able to find the standard deviation of a matrix by using matrix.std() method. #
1 min read
numpy.zeros() in Python
numpy.zeros() function creates a new array of specified shapes and types, filled with zeros. It is beneficial when you need a placeholder array to initialize variables or store intermediate results. We can create 1D array using numpy.zeros(). Let's understand with the help of an example: [GFGTABS] P
2 min read
numpy.array_str() in Python
numpy.array_str()function is used to represent the data of an array as a string. The data in the array is returned as a single string. This function is similar to array_repr, the difference being that array_repr also returns information on the kind of array and its data type. Syntax : numpy.array_st
2 min read
numpy.exp() in Python
numpy.exp(array, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : This mathematical function helps user to calculate exponential of all the elements in the input array. Parameters : array : [array_like]Input array or object whose elements, we need to test. out : [ndarray
4 min read
numpy.add() in Python
NumPy, the Python powerhouse for scientific computing, provides an array of tools to efficiently manipulate and analyze data. Among its key functionalities lies numpy.add() a potent function that performs element-wise addition on NumPy arrays. numpy.add() SyntaxSyntax : numpy.add(arr1, arr2, /, out=
4 min read
Python NumPy
Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Besides its obvious scientific uses, Numpy can also be used as an efficient
6 min read
numpy.exp2() in Python
numpy.exp2(array, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : This mathematical function helps user to calculate 2**x for all x being the array elements. Parameters : array : [array_like]Input array or object whose elements, we need to test. out : [ndarray, optional
2 min read
Numpy.prod() in Python
numpy.prod() returns the product of array elements over a given axis. Syntax: numpy.prod(a, axis=None, dtype=None, out=None, keepdims=) Parameters a : array_like Its the input data. axis : None or int or tuple of ints, its optional It is theAxis or axes along which a product is performed. The defaul
3 min read