NumPy ndarray.std()
The numpy.ndarray.std()
method computes the standard deviation of elements in a NumPy array.
It can calculate the standard deviation across the entire array or along a specified axis.
Syntax
</>
Copy
ndarray.std(axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, where=True)
Parameters
Parameter | Type | Description |
---|---|---|
axis | None, int, or tuple of ints, optional | Axis or axes along which the standard deviation is computed. If None , the standard deviation is computed for the entire array. |
dtype | dtype, optional | Data type for the computation. Defaults to float64 for integer arrays and the same type as the input array for floating-point arrays. |
out | ndarray, optional | Alternative output array where the result is stored. Must have the same shape as the expected output. |
ddof | int or float, optional | Delta Degrees of Freedom. The divisor used is N - ddof , where N is the number of elements. Defaults to 0 . |
keepdims | bool, optional | If True , the reduced dimensions are retained as size one, enabling proper broadcasting. |
where | array_like of bool, optional | Specifies elements to include in the standard deviation calculation. |
Return Value
Returns a float value if axis=None
, or an array of standard deviation values if an axis is specified.
Examples
1. Calculating Standard Deviation for an Entire Array
Computing the standard deviation for all elements in a NumPy array.
</>
Copy
import numpy as np
# Creating a 1D array
arr = np.array([1, 2, 3, 4, 5])
# Calculating the standard deviation of the entire array
result = arr.std()
print(result) # Output: Standard deviation of all elements
Output:
1.4142135623730951
2. Using the axis
Parameter in ndarray.std()
Computing the standard deviation along different axes in a 2D array.
</>
Copy
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Standard deviation along axis 0 (columns)
result_axis0 = arr.std(axis=0)
print(result_axis0)
# Standard deviation along axis 1 (rows)
result_axis1 = arr.std(axis=1)
print(result_axis1)
Output:
[1.5 1.5 1.5]
[0.81649658 0.81649658]
3. Using the ddof
Parameter in ndarray.std()
Modifying the degrees of freedom for standard deviation calculation.
</>
Copy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# Calculating standard deviation with ddof=1 (sample standard deviation)
result_ddof1 = arr.std(ddof=1)
print(result_ddof1)
Output:
1.5811388300841898
4. Keeping Dimensions with keepdims=True
in ndarray.std()
Using keepdims=True
to retain the reduced dimension as size one.
</>
Copy
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Keeping dimensions after standard deviation computation
result = arr.std(axis=1, keepdims=True)
print(result)
Output:
[[0.81649658]
[0.81649658]]
5. Using the where
Parameter in ndarray.std()
Calculating the standard deviation by selecting specific elements using a mask.
</>
Copy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
mask = np.array([True, False, True, False, True])
# Computing standard deviation for selected elements
result = arr.std(where=mask)
print(result)
Output:
1.632993161855452