# NumPy std()

The `std()` method computes the standard deviation of a given set of numbers along the specified axis.

### Example

``````import numpy as np

# create an array
array1 = np.array([0, 1, 2, 3, 4, 5, 6, 7])

# calculate the standard deviation of the array
deviation = np.std(array1)

print(deviation)

# Output: 2.29128784747792``````

## std() Syntax

The syntax of `std()` is:

``numpy.std(array, axis=None, dtype=None, out=None, ddof = 0, keepdims=<no value>, where=<no value>)``

## std() Arguments

The `std()` method takes the following arguments:

• `array` -array containing numbers whose standard deviation is desired(can be `array_like`)
• `axis`(optional)- axis or axes along which the standard deviation is computed(`int` or `tuple of int`)
• `dtype`(optional)- the datatype to use in the calculation of standard deviation(`datatype`)
• `out`(optional)- output array in which to store the result(`ndarray`)
• `ddof`(optional)- delta degrees of freedom(`int`)
• `keepdims`(optional)- specifies whether to preserve the shape of the original array(`bool`)
• `where`(optional)- filter the elements to include in the standard deviation(`array of bool`)

Notes:

The default values of,

• `axis = None`, i.e. array is flattened and the standard deviation of the entire array is taken.
• `dtype = None`, i.e. in the case of integers, `float` is taken otherwise standard deviation is of the same datatype as the elements
• By default, `keepdims` and `where` will not be passed.

## std() Return Value

The `std()` method returns the standard deviation of the array.

## Example 1: Find the Standard Deviation of ndArray

``````import numpy as np

# create an array
array1 = np.array([[[0, 1], [2, 3]],
[[4, 5],  [6, 7]]])

# find the standard deviation of entire array
deviation1 = np.std(array1)

# find the standard deviation across axis 0 (slice wise mean)
deviation2 = np.std(array1, 0)

# find the standard deviation across axis 0 and 1
deviation3 = np.std(array1, (0, 1))

print('\nStandard Deviation of the entire array:', deviation1)
print('\nStandard Deviation across axis 0:\n', deviation2)
print('\nStandard Deviation across axis 0 and 1', deviation3)``````

Output

```Standard Deviation of the entire array: 2.29128784747792

Standard Deviation across axis 0:
[[2. 2.]
[2. 2.]]

Standard Deviation across axis 0 and 1 [2.23606798 2.23606798]```

When no `axis` parameter is specified, `np.std(array1)` calculates the standard deviation of the entire array.

Calculating the standard deviation along `axis=0` gives the standard deviation across the rows for each column.

Calculating the standard deviation along `axis=(0, 1)` gives the standard deviation simultaneously across the rows and columns. The resulting array is a 1D array with the standard deviation of all elements in the entire 2D array

## Example 2: Specify Datatype of Standard Deviation of a ndArray

We can use `dtype` argument to specify the data type of the output array.

``````import numpy as np

array1 = np.array([[1, 2, 3],
[4, 5, 6]])

# by default int is converted to float
result1 = np.std(array1)

# pass dtype to specify integer output
result2 = np.std(array1, dtype = int)

print('Float deviation:', result1)
print('Integer deviation:', result2)``````

Output

```Float deviation: 1.707825127659933
Integer deviation: 1```

Note: Using a lower precision `dtype`, such as `int`, can lead to a loss of accuracy.

## Example 3: Using Optional keepdims Argument

If `keepdims` is set to `True`, the dimension of the original array is preserved and passed to the resultant standard deviation array.

``````import numpy as np

array1 = np.array([[1, 2, 3],
[4, 5, 6]])

# keepdims defaults to False
result1 = np.std(array1, axis = 0)

# pass keepdims as True
result2 = np.std(array1, axis = 0, keepdims = True)

print('Original Array Dimension:', array1.ndim)
print('Standard Deviation without keepdims:', result1, 'Dimensions', result1.ndim)
print('Standard Deviation with keepdims:', result2, 'Dimensions', result2.ndim)``````

Output

```Original Array Dimension: 2
Standard Deviation without keepdims: [1.5 1.5 1.5] Dimensions 1
Standard Deviation with keepdims: [[1.5 1.5 1.5]] Dimensions 2```

## Example 4: Standard Deviation of Filtered Array Using where()

We can filter the array using the `where` argument and find the standard deviation of the filtered array.

``````import numpy as np

array1 = np.array([[1, 2, 3],
[4, 5, 6]])

# standard deviation of entire array
result1 = np.std(array1)

# standard deviation of only even elements
result2 = np.std(array1, where = (array1%2==0))

# standard deviation of  numbers greater than 3
result3 = np.std(array1, where = (array1 > 3))

print('Standard Deviation of entire array:', result1)
print('Standard Deviation of only even elements:', result2)
print('Standard Deviation of  numbers greater than 3:', result3)``````

Output

```Standard Deviation of entire array: 1.707825127659933
Standard Deviation of only even elements: 1.632993161855452
Standard Deviation of  numbers greater than 3: 0.816496580927726```

## Example 5: Use out to Store the Result in Desired Location

The `out` parameter allows to specify an output array where the result will be stored.

``````import numpy as np

array1 = np.array([[1, 2, 3],
[4, 5, 6]])

# create an output array
output = np.zeros(3)

# compute standard deviation and store the result in the output array
np.std(array1, out = output, axis = 0)

print('Standard Deviation:', output)``````

Output

`Standard Deviation: [1.5 1.5 1.5]`

What is standard deviation?

The standard deviation is the measure of how dispersed the data is in relation to the mean. In our case, the standard deviation measures the spread of values around the given array's mean value.

Mathematically,

``std = sqrt(sum((arr - arr.mean())**2) / (N - 1))``

In NumPy,

``````import numpy as np

array1 = np.array([2, 4, 6, 8, 10])

# calculate standard deviation using np.std()
deviation1 = np.std(array1)

# calculate standard deviation without using np.std()
mean = np.mean(array1)
diff_squared = (array1 - mean) ** 2
variance = np.mean(diff_squared)
deviation2 = np.sqrt(variance)

print('Standard Deviation with np.std():', deviation1)
print('Standard Deviation without np.std():', deviation2)``````

Output

```Standard Deviation with np.std(): 2.8284271247461903
Standard Deviation without np.std(): 2.8284271247461903```
What is the `ddof` parameter in `np.std()` used for?

The `ddof` (Delta Degrees of Freedom) parameter in `np.std()` allows adjusting the divisor used in the calculation of standard deviation. The default value is 0, which corresponds to dividing by N, the number of elements.

In the above formula of std,

``std = sqrt(sum((arr - arr.mean())**2) / (N - ddof))``

Let's see an example.

``````import numpy as np

array1 = np.array([1, 2, 3, 4, 5])

# calculate standard deviation with the default ddof=0
deviation1 = np.std(array1)

# calculate standard deviation with ddof=1
deviation2 = np.std(array1, ddof=1)

print('Standard Deviation (default ddof=0):', deviation1)
print('Standard Deviation (ddof=1):', deviation2)``````

Output

```Standard Deviation (default ddof=0): 1.4142135623730951
Standard Deviation (ddof=1): 1.5811388300841898```