Updated June 7, 2023

## Introduction to NumPy Standard Deviation

The standard deviation is the square root of the average square deviation from the mean. In Python’s NumPy module, you can use the numpy.std() function to calculate the standard deviation along a specified axis. It returns the standard deviation of the array elements, and by default, it calculates the standard deviation of a flattened array. The formula to calculate the average square deviation of an array x is x.sum/N, where N is the array x’s length. The standard deviation is calculated using the formula Standard Deviation = sqrt(mean(abs(x – x.mean())**2)).

**Syntax:**

`numpy.std(arrayname, axis=None, dtype=None, out=None, ddof=0, keepdims=<class numpy._globals._NoValue>)`

Where,

- arrayname is the name of the array whose element’s standard deviation is to be calculated.
- axis specifies the axis along which the standard deviation must be calculated; this value is optional.
- dtype specifies the data type used to compute the standard deviation; this value is optional.
- out specifies the name of the output array in which the result of the given array will be stored, and this value is optional.
- ddof specifies the delta degrees of freedom, and this value is optional.

### Working of NumPy standard deviation

- The standard deviation is the square root of the average squared deviation from the mean.
- The NumPy module in Python provides the numpy.std() function, which you can use to calculate the standard deviation along a specified axis.
- The numpy.std() function in Python’s NumPy module returns the standard deviation of the array elements.
- By default, the numpy.std() function in Python’s NumPy module calculates the standard deviation of the flattened array.
- The formula used to calculate the average square deviation of a given array x is x.sum/N where N is the length of the array x, and the standard deviation is calculated using the formula Standard Deviation=sqrt(mean(abs(x-x.mean( ))**2.

### Examples

Given below are the examples:

#### Example #1

Python program demonstrating the NumPy std function by creating an array with the NumPy array function and calculating the standard deviation of the array’s items with the NumPy std() function.

**Code:**

```
#importing the package numpy
import numpy as no
#Creating an array by making use of array function in NumPy and storing it in a variable called arrayname
arrayname = no.array([[1,2],[3,4]])
#Displaying the elements of arrayname followed by one line space by making use of \n
print 'The elements of the given array are:'
print arrayname
print '\n'
#using std function of NumPy and passing the created array as the parameter to that function to find the standard deviation value of all the elements in the array and store it in a variable called stddev
stddev = no.std(arrayname)
#Displaying the standard deviation value stored in stddev variable
print 'The standard deviation of all the elements of the array is:'
print stddev
```

**Output:**

In the above program, to utilize the array and std function, we import the NumPy package in Python. Then we use the array function to create an array stored in the variable called arrayname. We then display the elements of the array arrayname on the screen. Then we pass the created arrayname as the parameter to the std function to find the standard deviation value stored in a variable called stddev. Then, we display the standard deviation value stored in the stddev variable as the output on the screen.

#### Example #2

Python program demonstrating the NumPy std function by creating an array with the NumPy array function and calculating the standard deviation of the array’s items with the NumPy std() function.

**Code:**

```
#importing the package numpy
import numpy as no
#Creating an array by making use of array function in NumPy and storing it in a variable called arrayname
arrayname = no.array([[1.1,2.1,3.1],[4.1,5.1,6.1]])
#Displaying the elements of arrayname followed by one line space by making use of \n
print 'The elements of the given array are:'
print arrayname
print '\n'
#using std function of NumPy and passing the created array as the parameter to that function to find the standard deviation value of all the elements in the array and store it in a variable called stddev
stddev = no.std(arrayname)
#Displaying the standard deviation value stored in stddev variable
print 'The standard deviation of all the elements of the array is:'
print stddev
```

**Output:**

In the above program, we import the NumPy package in Python to utilize the array and std functions. Then we use the array function to create an array stored in the variable called arrayname. We then display the elements of the array arrayname on the screen. Then we pass the created arrayname as the parameter to the std function to find the standard deviation value stored in a variable called stddev. Then, we display the standard deviation value stored in the stddev variable as the output on the screen.

#### Example #3

Python program demonstrating the NumPy std function by creating an array with the NumPy array function and calculating the standard deviation of the array’s items with the NumPy std() function.

**Code:**

```
#importing the package numpy
import numpy as no
#Creating an array by making use of array function in NumPy and storing it in a variable called arrayname
arrayname = no.array([[11,12],[14,15]])
#Displaying the elements of arrayname followed by one line space by making use of \n
print 'The elements of the given array are:'
print arrayname
print '\n'
#using std function of NumPy and passing the created array as the parameter to that function to find the standard deviation value of all the elements in the array and store it in a variable called stddev
stddev = no.std(arrayname)
#Displaying the standard deviation value stored in stddev variable
print 'The standard deviation of all the elements of the array is:'
print stddev
```

**Output:**

In the above program, to utilize the array and std function, we import the NumPy package in Python. Then we use the array function to create an array stored in the variable called arrayname. We then display the elements of the array arrayname on the screen. Then we pass the created arrayname as the parameter to the std function to find the standard deviation value stored in a variable called stddev. Finally, we display the standard deviation value stored in the stddev variable as the output on the screen.

### Recommended Articles

We hope that this EDUCBA information on “NumPy Standard Deviation” was beneficial to you. You can view EDUCBA’s recommended articles for more information.