Skip to content
Related Articles

Related Articles

Calculate the average, variance and standard deviation in Python using NumPy
  • Last Updated : 25 Aug, 2020
GeeksforGeeks - Summer Carnival Banner

Numpy in Python is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Numpy provides very easy methods to calculate the average, variance, and standard deviation.

Average

Average a number expressing the central or typical value in a set of data, in particular the mode, median, or (most commonly) the mean, which is calculated by dividing the sum of the values in the set by their number. The basic formula for the average of n numbers x1, x2, ……xn is

A = (x_1 + x_2 ........ + x_n)/ n

Example:

Suppose there are 8 data points,



2,\ 4,\ 4,\ 4,\ 5,\ 5,\ 7,\ 9

The average of these 8 data points is,

A = \frac{2 + 4 + 4 + 4 + 5 + 5 + 7 + 9}{8} = 5

Average in Python Using Numpy:

One can calculate the average by using numpy.average() function in python.

Syntax: 

numpy.average(a, axis=None, weights=None, returned=False)

Parameters:

a: Array containing data to be averaged



axis: Axis or axes along which to average a

weights: An array of weights associated with the values in a

returned: Default is False. If True, the tuple is returned, otherwise only the average is returned

Example 1:

Python




# Python program to get average of a list
  
# Importing the NumPy module
import numpy as np
  
# Taking a list of elements
list = [2, 4, 4, 4, 5, 5, 7, 9]
  
# Calculating average using average()
print(np.average(list))

Output:

5.0

Example 2:

Python




# Python program to get average of a list
  
# Importing the NumPy module
import numpy as np
  
# Taking a list of elements
list = [2, 40, 2, 502, 177, 7, 9]
  
# Calculating average using average()
print(np.average(list))

Output:

105.57142857142857

Variance

Variance is the sum of squares of differences between all numbers and means. The mathematical formula for variance is as follows,

Formula: \sigma^{2}= \frac { \sum_{i=1}^{N} (x_{i}-\mu)^{2}}{N}

Where,

 ? is Mean, 

N is the total number of elements or frequency of distribution. 

Example:

Let’s consider the same dataset that we have taken in average. First, calculate the deviations of each data point from the mean, and square the result of each,

\begin{array}{lll} (2-5)^2 = (-3)^2 = 9 && (5-5)^2 = 0^2 = 0 \\ (4-5)^2 = (-1)^2 = 1 && (5-5)^2 = 0^2 = 0 \\ (4-5)^2 = (-1)^2 = 1 && (7-5)^2 = 2^2 = 4 \\ (4-5)^2 = (-1)^2 = 1 && (9-5)^2 = 4^2 = 16. \\ \end{array}
variance = \frac{9 + 1 + 1 + 1 + 0 + 0 + 4 + 16}{8} = 4

Variance in Python Using Numpy:

One can calculate the variance by using numpy.var() function in python.

Syntax: 

numpy.var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>)

Parameters:

a: Array containing data to be averaged

axis: Axis or axes along which to average a

dtype: Type to use in computing the variance. 

out: Alternate output array in which to place the result.

ddof: Delta Degrees of Freedom

keepdims: If this is set to True, the axes which are reduced are left in the result as dimensions with size one

Example 1:

Python




# Python program to get variance of a list
  
# Importing the NumPy module
import numpy as np
  
# Taking a list of elements
list = [2, 4, 4, 4, 5, 5, 7, 9]
  
# Calculating variance using var()
print(np.var(list))

Output:

4.0

Example 2:

Python




# Python program to get variance of a list
  
# Importing the NumPy module
import numpy as np
  
# Taking a list of elements
list = [212, 231, 234, 564, 235]
  
# Calculating variance using var()
print(np.var(list))

Output:

18133.359999999997

Standard Deviation

Standard Deviation is the square root of variance. It is a measure of the extent to which data varies from the mean. The mathematical formula for calculating standard deviation is as follows, 

Standard Deviation = \sqrt{ variance }

Example:

Standard Deviation for the above data,

Standard Deviation = \sqrt{ 4 } = 2

Standard Deviation in Python Using Numpy:

One can calculate the standard devaition by using numpy.std() function in python.

Syntax: 

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

Parameters:

a: Array containing data to be averaged

axis: Axis or axes along which to average a

dtype: Type to use in computing the variance. 

out: Alternate output array in which to place the result.

ddof: Delta Degrees of Freedom

keepdims: If this is set to True, the axes which are reduced are left in the result as dimensions with size one

Example 1:

Python




# Python program to get 
# standard deviation of a list
  
# Importing the NumPy module
import numpy as np
  
# Taking a list of elements
list = [2, 4, 4, 4, 5, 5, 7, 9]
  
# Calculating standard 
# deviation using var()
print(np.std(list))

Output:

2.0

Example 2:

Python




# Python program to get
# standard deviation of a list
  
# Importing the NumPy module
import numpy as np
  
# Taking a list of elements
list = [290, 124, 127, 899]
  
# Calculating standard
# deviation using var()
print(np.std(list))

Output:

318.35750344541907

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :