NumPy std() in Python

Share this article

In NumPy, std() is a method used to calculate the standard deviation of an array’s elements. Standard deviation is a measure of how spread out the numbers in a dataset are, giving insight into the variability or dispersion within the data. This method is part of the NumPy library, commonly used for numerical computations in Python.

Key Takeaway

numpy.std() computes the standard deviation, which indicates the extent of variation or distribution of dataset values. A small standard deviation means values are close to the mean, while a large one indicates a wide range of values.

Example

import numpy as np

data = [10, 20, 30, 40, 50]
result = np.std(data)
print(result)  # Output: 14.142135623730951

What it does

The np.std() function calculates the standard deviation of the given data array. It measures the average amount by which each data point deviates from the mean of the dataset.

  • Low standard deviation: Indicates that data points are close to the mean.
  • High standard deviation: Indicates more variation or spread in the data points.

Examples

Example 1: Basic usage with an array

import numpy as np

arr = [1, 2, 3, 4, 5]
std_dev = np.std(arr)
print(std_dev)  # Output: 1.4142135623730951

When passed an array, np.std() calculates the standard deviation, showing how much the numbers in the array vary from the mean. Here, it returns approximately 1.41, indicating moderate dispersion.

Example 2: Using with multi-dimensional arrays

import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
std_dev = np.std(matrix)
print(std_dev)  # Output: 1.707825127659933

For multi-dimensional arrays, np.std() computes the standard deviation of all elements in the array by default. Here, it calculates the dispersion of all values in the 2D array.

Example 3: Specifying the axis

import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
std_dev_axis0 = np.std(matrix, axis=0)
print(std_dev_axis0)  # Output: [1.5 1.5 1.5]

By specifying the axis parameter, you can calculate the standard deviation along a particular axis. In this case, axis=0 computes the standard deviation of each column in the 2D array.

Example 4: Using ddof for sample standard deviation

import numpy as np

data = [10, 20, 30, 40, 50]
sample_std_dev = np.std(data, ddof=1)
print(sample_std_dev)  # Output: 15.811388300841896

By setting the ddof (Delta Degrees of Freedom) parameter to 1, you get the sample standard deviation instead of the population standard deviation. This is commonly used in statistical analyses when dealing with a sample subset of data.