# Numpy – Get Standard Deviation of Array Values

The Numpy library in Python comes with a number of useful built-in functions for computing common descriptive statistics like mean, median, standard deviation, etc. In this tutorial, we will look at how to get the standard deviation of a Numpy array with the help of some examples.

## How do you get the standard deviation of an array in Numpy?

You can use the Numpy `std()` function to get the standard deviation of the values in a Numpy array. Pass the array as an argument.

The following is the syntax –

```# standard deviation of all values in array
numpy.std(ar)```

It returns the standard deviation taking into account all the values in the array. For multi-dimensional arrays, you can specify the axis along which you want to compute the standard deviation (see the examples below).

## Examples

Let’s now look at some examples of using the above syntax on single and multi-dimensional arrays.

Highlighted programs for you

Flatiron School

Flatiron School

Data Science Bootcamp
Product Design UX/UI Bootcamp

University of Maryland Global Campus

University of Maryland Global Campus

Cloud Computing Systems Master's
Digital Forensics & Cyber Investigation Master's

Creighton University

Creighton University

Health Informatics Master's

### Example 1 – Standard deviation of a one-dimensional Numpy array

Let’s first create a one-dimensional Numpy array.

```import numpy as np

# create numpy array
ar = np.array([1, 2, 3, 4])
# display the array
print(ar)```

Output:

`[1 2 3 4]`

Here, we used the `numpy.array()` function to create a one-dimensional array containing some numeric values.

Let’s now get the standard deviation of all the values in the above array.

```# std dev of array
print(np.std(ar))```

Output:

`1.118033988749895`

We get the standard deviation as approximately 1.118

### Example 2 – Standard deviation of multi-dimensional Numpy array

First, let’s create a 2-D Numpy array.

```# create 2-D numpy array
ar = np.array([[1, 2, 3],
[2, 1, 1]])
# display the array
print(ar)```

Output:

```[[1 2 3]
[2 1 1]]```

Here, we used the `numpy.array()` function to create a Numpy array with two rows and three columns.

If you use the Numpy `std()` function on an array without specifying the axis, it will return the standard deviation taking into account all the values inside the array.

```# std dev of array
print(np.std(ar))```

Output:

`0.7453559924999299`

We get the standard deviation of all the values inside the 2-D array.

Use the `numpy.std()` function with `axis=1` to get the standard deviation for each row in the array.

```# std dev of each row in array
print(np.std(ar, axis=1))```

Output:

`[0.81649658 0.47140452]`

We get the standard deviation of each row in the above 2-D array. The standard deviation of the values in the first row (1, 2, 3) is 0.816 and the standard deviation of the values in the second row (2, 1, 1) is 0.471.

Use the `numpy.std()` function with `axis=0` to get the standard deviation of each column in the array.

```# std dev of each column in array
print(np.std(ar, axis=0))```

Output:

`[0.5 0.5 1. ]`

We get the standard deviation of each column in the above 2-D array. The standard deviation of the values – in the first column (1, 2) is 0.5, in the second column (2, 1) is 0.5, and in the third column (3, 1) is 1.

## Summary

In this tutorial, we looked at how to use the `numpy.std()` function to get the standard deviation of values in an array. The following are the key takeaways from this tutorial.

• Use the `numpy.std()` function without any arguments to get the standard deviation of all the values inside the array.
• For multi-dimensional arrays, use the `axis` parameter to specify the axis along which to compute the standard deviation. For example, for a 2-D array –
• Pass `axis=1` to get the standard deviation of each row.
• Pass `axis=0` to get the standard deviation of each column.

You might also be interested in –

• 