Python variance() function
Variance
The variance is the average of the square deviations from the mean. The variance will measure the spread of the dataset from its mean or median value. The greater the variance value, the larger the data spread, and the lower the variance value the data will be in the group from that is, the data is not spread apart from the mean value. The variance is equal to the square of the standard deviation. The variance is generally used in analyzing the data. The square of the standard deviation is also known as the second central moment of the given data.
Steps to Calculate the Variance of Data
- Calculate the mean of the given data.
- Subtract the given numbers from the calculated mean.
- Squaring the obtaining values.
- Add all the values obtained together.
- Now divide the total result obtained by the total number of observations in the data.
Example
- The variance of the population tells us how the people are spread over a given area about an average value.
- The variance of the number of trees in a particular area will give us the number of trees spread in a given area.
Python Variance Function
The python variance( ) function is used to calculate the variance of the given sample of the data. The python consists of the python statistics module, which provides all the functions related to data statics. Python also provides another function, variance( ), used to calculate the variance of the population.
Syntax:
statistics.variance(data, xbar=None)
Parameters:
data- Provides the data of which we need to find the variance.
xbar- It is an optional parameter that collects the data's mean.
Return value- This function will return the variance of the given data.
Steps to calculate variance( ) using python
- First, we need to import the statistics module
- Input the data of which variance is to be calculated.
- Use the variance( ) function to calculate the variance of the given data.
- Print the variance obtained.
Example
#importing thestatistics library
importstatistics
numbers=[5,7,9,12, 15]
#calculating the variance of the numbers
variance=staistics.variance(numbers)
#displaying the variance of the data
print(variance)
Output
15.8
We can also calculate the variance of the data by passing both the arguments that bypass the data and the mean value of the data together.
Example:
#importing libraries
import statistics
numbers=[5,7,9,12, 15]
#calculating the mean of the data
mean=statistics.mean(numbers)
#calculating the variance of the numbers
variance=staistics.variance(numbers, mean)
#displaying the variance of the data
print(variance)
Output
15.8
Example
#importing module
import statistics
#creating a list
value = (2,5,6,9,11)
#calculating the mean by using the mean( ) method
mean=statistics.mean(value)
#calculating the variance of data
var=statistics.variance(value, xbar=6.3)
#displaying the variance of the data with different x-bar value
print(var)
Output
12.28
Here we can observe that by changing the x-bar value, the variance of the data is also changed; that is, the variance of the original data is 12.3, but by changing the x-bar value, the variance changed to 12.8. So, we can say that by changing the x-bar value and not keeping the original mean value, the variance of the data is also changed.
Variance of the Fraction Values
In python, we can also calculate the variance of the fraction values. To perform this operation, we need to follow the following steps-
- Import the statistics module and the decimal module.
- Collect the data, which is in decimal form.
- Calculate the variance of the data.
- Print the variance of the data.
Example
#Importing the libraries
from decimal import decimal as D
fromstatistics import variance
#Collecting the data in decimal format
data=[D(“10.11”), D(“17.13”),D(“21.12”),D(“15.25”),D(“13.87”)]
#Calculating the variance of the data
variance=variance(data)
#Printing the data
print(variance)
Output
16.54433
Variance using Numpy
We can also calculate the variance of the data by importing the numpy module. The numpy will accept the data as an array so that we can calculate the variance of an array.
Syntax:
np.var(data, xbar=None)
Parameters
Data: Input the data in the form of the numpy array.
Xbar: It generally takes the mean of the data as the input. It is an optional parameter.
Example:
#importing the module
importnumpy as np
data=[12,14,16,38,34]
#calculating the variance of the data
variance=np.var(data)
#displaying the variance of the given data
print( variance )
Output
149.2
Here in this example, we have calculated the variance of the numpy array by using np.var( ) to calculate the variance of the given numpy array.