NumPy std()

Summary: in this tutorial, you’ll learn how to use the numpy std() function to calculate the standard deviation.

Standard deviation measures how spread out the elements of an array is. The more spread out elements is, the greater their standard deviation.

Standard deviation is the square root of the variance. To calculate the variance, check out the numpy var() function tutorial.

To calculate standard deviation, you can use the numpy std() function as follows:

numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>, *, where=<no value>)Code language: Python (python)

The std() function has many parameters but we’ll focus on only the first one in this tutorial.

NumPy std() function example

Suppose you have a list of trees with the broadest crown. The first column displays the tree name and the second column shows its corresponding diameter in feet:

Tree NameDiameter (Feet)
Thimmamma Marrimanu591
Monkira Monster239
Oriental Plane Tree at Corsham Court210
Saman de Guere207
The Big Tree201
Shugborough Yew182
Moreton Bay Fig Tree176
The Pechanga Great Oak176
El Gigante175
Benaroon170
The E. O. Hunt Oak170
The Lansdowne Sycamore169
The Glencoe Tree168

The following example uses the std() function to calculate the standard deviation of the diameters of the above trees:

import numpy as np


diameters = np.array([591, 239, 210, 207, 201, 182,
                      176, 176, 175, 170, 170, 169, 168, ])
result = np.std(diameters)
print(round(result, 1))Code language: Python (python)

Output:

109.6Code language: Python (python)

How it works.

First, create an array that holds the diameters of trees:

diameters = np.array([591, 239, 210, 207, 201, 182,
                      176, 176, 175, 170, 170, 169, 168, ])Code language: Python (python)

Second, calculate the standard deviation of diameters using the std() function:

result = np.std(diameters)Code language: Python (python)

Third, round the standard deviation and display it:

print(round(result, 1))Code language: Python (python)

By using the standard deviation, we have a “standard” way of knowing which trees have a normal diameter, and which trees have large or small diameters.

Summary

  • Use the numpy std() function to calculate the standard deviation.
Did you find this tutorial helpful ?