![]() |
VOOZH | about |
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.
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..
numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False)
Parameters:
Return Value: The standard deviation of the elements in the array, computed along the specified axis or over the entire array.
This example demonstrates how to calculate the standard deviation of a 1D array using numpy.std().
arr : [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.
This example demonstrates how to compute the standard deviation of a 2D array using numpy.std().
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.