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.
\text{Standard Deviation} = \sqrt{\text{mean} \left( (x - x.\text{mean}())^2 \right)}
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.sqrt() in Python
numpy.sqrt() in Python is a function from the NumPy library used to compute the square root of each element in an array or a single number. It returns a new array of the same shape with the square roots of the input values. The function handles both positive and negative numbers, returning NaN for n
2 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. P
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:Pythonimport
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.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 m
6 min read
numpy.load() in Python
numpy.load() function return the input array from a disk file with npy extension(.npy). Syntax : numpy.load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding='ASCII') Parameters: file : : file-like object, string, or pathlib.Path.The file to read. File-like objects must support the
2 min read
Number System in Python
The arithmetic value that is used for representing the quantity and used in making calculations is defined as NUMBERS. The writing system for denoting numbers logically using digits or symbols is defined as a Number system. Number System is a system that defines numbers in different ways to represen
6 min read
Numpy size() function | Python
numpy.size() function in Python is used to count the number of elements in a NumPy array. You can use it to get the total count of all elements, or to count elements along a specific axis, such as rows or columns in a multidimensional array. This makes it useful when quickly trying to understand the
2 min read
NumPy Array in Python
NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C
2 min read