numpy
  1. numpy-numpystd

numpy.std()

The numpy.std() function in NumPy returns the standard deviation of the given array elements along a specified axis, or of the flattened array if no axis is specified. The standard deviation is a measure of the variability or spread of a set of data points.

Syntax

The syntax for using numpy.std() function is:

numpy.std(arr, axis=None, dtype=None, ddof=0, keepdims=<no value>)

Here,

  • arr: Required. The array for which the standard deviation is to be calculated.
  • axis: Optional. The axis along which the standard deviation is to be calculated. Default is None which calculates the standard deviation of the flattened array.
  • ddof: Optional. Degree of Freedom. By default, it is set to 0.
  • keepdims: Optional. If this is set to True, then the specified axis will be retained as an axis of length 1 in the output.

Example

Let's take an example of calculating the standard deviation of an array using numpy.std():

import numpy as np

arr = np.array([4, 5, 6, 7, 8, 9, 10])
result = np.std(arr)

print(result)

Output:

2.0

In this example, we are calculating the standard deviation of the given array [4, 5, 6, 7, 8, 9, 10] using numpy.std() function. Since no axis is specified, the function calculates the standard deviation of the flattened array.

Explanation

The numpy.std() function in NumPy calculates the standard deviation of the given array elements along a specified axis, or of the flattened array if no axis is specified. It represents the variability of a set of data points.

The formula to calculate the standard deviation is:

standard deviation formula

where μ is the mean of the dataset, xi is each element of the dataset, and N is the total number of elements in the dataset.

Use

The numpy.std() function is useful in scientific and statistical analysis, data modeling, quality control, and various other applications where you need to measure the spread or variability of the data.

Important Points

  • The numpy.std() function returns a scalar value when no axis is specified, and an array when the axis is specified.
  • The standard deviation of a dataset tends to increase as the spread of the dataset increases, and vice versa.
  • The standard deviation of a normal distribution is sometimes referred to as the population standard deviation, while the standard deviation of a sample is referred to as the sample standard deviation.

Summary

In NumPy, the numpy.std() function returns the standard deviation of the given array elements along a specified axis, or of the flattened array if no axis is specified. It is useful in scientific and statistical analysis to measure the spread or variability of the data. We can also customize the degree of freedom using the ddof parameter.

Published on: