NumPy – Understanding ndarray.std() method (4 examples)

Updated: February 27, 2024 By: Guest Contributor Post a comment

Introduction

This tutorial dives deep into one of the core functions available in NumPy: std() method of ndarray objects. The standard deviation measures how spread out the numbers in a data set are. In NumPy, the std() method allows users to compute the standard deviation along specific axes of an array, embodying an essential tool for data analysis and scientific computing. We will explore the std() method through four progressively advanced examples, detailing how to interpret its results and integrate it into your numerical computations.

Syntax & Parameters

Syntax:

numpy.ndarray.std(axis=None, dtype=None, out=None, ddof=0)

Parameters:

  • axis: (Optional) Axis or axes along which the standard deviation is computed. By default, the standard deviation is computed over the flattened array.
  • dtype: (Optional) Data type of the returned array. If not specified, the data type of the array is used.
  • out: (Optional) Output array where the result is placed.
  • ddof: (Optional) Delta degrees of freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. By default, ddof is 0.

Example 1: Basic Usage of std()

This example shows the simplest use of ndarray.std(). Consider an array A with some numbers:

import numpy as np
A = np.array([1, 2, 3, 4, 5])
std_val = A.std()
print("Standard Deviation:", std_val)

Output:

Standard Deviation: 1.4142135623730951

This output represents the population standard deviation of the array A, illustrating the basic use of the method.

Example 2: Using the ddof Parameter

The std() method takes an optional parameter ddof, which stands for Delta Degrees of Freedom. By default, ddof is set to 0, implying a population standard deviation. Adjusting ddof allows for the calculation of the sample standard deviation. Let’s look at how this might change our calculation:

import numpy as np
A = np.array([1, 2, 3, 4, 5])
std_val_sample = A.std(ddof=1)
print("Sample Standard Deviation:", std_val_sample)

Output:

Sample Standard Deviation: 1.5811388300841898

Changing ddof to 1 adjusts the calculation to reflect the standard deviation of a sample rather than an entire population.

Example 3: Multidimensional Arrays

NumPy’s std() method shines when working with multidimensional arrays. It allows computation of standard deviation across different axes, enabling a deeper analysis of data variability. Consider the following 2D array:

import numpy as np
B = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
std_val_axis0 = B.std(axis=0)
std_val_axis1 = B.std(axis=1)
print("Std deviation across rows (axis=0):", std_val_axis0)
print("Std deviation across columns (axis=1):", std_val_axis1)

Output:

Std deviation across rows (axis=0): [2.44948974 2.44948974 2.44948974]
Std deviation across columns (axis=1): [0.81649658 0.81649658 0.81649658]

This demonstrates how the std() method can evaluate the data’s spread in different dimensions of an array.

Example 4: Complex Analysis using keepdims Parameter

The final example takes the utility of the std() method a step further. Use of the keepdims parameter allows the output to maintain the dimensions of the input array, crucial for certain types of data processing and analysis. Consider:

import numpy as np
C = np.array([[1, 2], [3, 4]])
std_val_keepdims = C.std(axis=1, keepdims=True)
print("Standard Deviation with keepdims=True:\n", std_val_keepdims)

Output:

Standard Deviation with keepdims=True:
 [[0.5]
 [0.5]]

This illustrates how keeping dimensionality can aid in subsequent calculations or data manipulations that depend on the original array shape.

Conclusion

Through these examples, we’ve seen the versatility and power of the ndarray.std() method. Whether you’re working with simple or complex data structures, NumPy provides an efficient way to compute standard deviations, helping to uncover the variability and trends in your data. Embracing these techniques can significantly enhance your data analysis capabilities.