# Compute the mean, standard deviation, and variance of a given NumPy array

Mean, Standard Deviation and Variance are the statistical measures which are used to describe the distribution of the given dataset data. In Numpy library, we have the functions to calculate the mean, standard deviation and variance of the arrays. Let’s see one by one in detail.

## Mean

Mean is also known as average, which is the sum of all the elements in the array divided by the total number of elements. It is used to represent the central tendency of the data.

### Syntax

Following is the syntax for applying the mean function on the arrays -

numpy.mean(arr)


Where

• Numpy is the library.

• mean is the function.

• arr is the input array.

### Example

In the following example, we are using the mean() function for calculating the mean of the given input 1-d array.

import numpy as np
a = np.array([22,1,7,14,5,2])
print("The input array:",a)
mean_array = np.mean(a)
print("The mean of the given input array:",mean_array)


### Output

Following is the output of the mean of the given input array.

The input array: [22 1 7 14 5 2]
The mean of the given input array: 8.5


### Example

Let’s see another example for calculating the mean of the 2-d array using the mean() function of the Numpy array.

import numpy as np
a = np.array([[34,23],[90,34],[43,23],[10,34]])
print("The input array:",a)
mean_array = np.mean(a)
print("The mean of the given input 2-d array:",mean_array)


### Output

Following is the output of the above code, when we run the code -

The input array: [[34 23]
[90 34]
[43 23]
[10 34]]
The mean of the given input 2-d array: 36.375


### Example

Here, we are calculating the mean of 3-d array using the mean() function of the Numpy array.

import numpy as np
a = np.array([[[34,23],[90,34]],[[43,23],[10,34]]])
print("The input array:",a)
mean_array = np.mean(a)
print("The mean of the given input 3-d array:",mean_array)


### Output

The input array: [[[34 23]
[90 34]]

[[43 23]
[10 34]]]
The mean of the given input 3-d array: 36.375


## Variance

This is used to find how the data was spread in the given dataset. It is calculated as the average of the squared differences of each data point from the mean of them. Mathematically the formula for finding the variance is given as below.

Variance = (1/n) * sum((xi - mean)2)


Where,

• n is the number of data points.

• xi is the ith data point of the given data.

• mean is average of the given data

• high variance

## High Variance

A high variance indicates that the data values are spread out over a large range, while the low variance indicates that the values are clustered around the mean of the data values. In Numpy we have the var() function to calculate the variance of the given array.

### Syntax

Following is the syntax for using the variance function on the arrays.

numpy.var(arr)


Where,

• Numpy is the library.

• var is the function.

• arr is the input array.

### Example

In the following example, we are trying to calculate the variance of a 1-d array using the var() function of the numpy –

import numpy as np
a = np.array([2,7,50,3,12])
print("The input array:",a)
variance = np.var(a)
print("The variance of the given input 1-d array:", variance)


### Output

The input array: [ 2  7 50  3 12]
The variance of the given input 1-d array: 322.16


### Example

Let’s see another example, which calculates the variance of the 2-d array using the var() function.

import numpy as np
a = np.array([[90,34],[43,23]])
print("The input array:",a)
variance = np.var(a)
print("The variance of the given input 2-d array:",variance)


### Output

The input array: [[90 34]
[43 23]]
The variance of the given input 2-d array: 652.25


## Standard Deviation

Standard deviation defines the measure of how the data is spread from the mean and tells us how much the data deviates from the mean. The mathematical formula for this method is as follows.

Standard deviation = √(Σ(xi - x)2 / (n - 1))


Where,

• n is the number of data points.

• Σ(xi - x)2 is the sum of the squared differences between the each data value and the mean.

### Standard deviation using Python

In statistics module of python, we have the function namely stdev() to find the standard deviation of the given array.

#### Example

In the following example, we are trying to calculate the standard deviation of an array by passing the 1-d array to the stdev() function –

import statistics
import numpy as np
a = np.array([34,23,90,34,90,34,43,23])
print("The input array:",a)
std = statistics.stdev(a)
print("The standard deviation of the given input 1-d array:",std)


#### Output

Following is the output of the standard deviation calculated for the given input array.

The input array: [34 23 90 34 90 34 43 23]
The standard deviation of the given input 1-d array: 27.694764848252458


Updated on: 07-Aug-2023

141 Views