×

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.


Related Topics

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

Handling missing keys in Python dictionaries

In this lesson, you will discover how to create a Python application that will manage missing keys in a dictionary. Python dictionaries store data as key-value pairs, where each value...

3 minutes read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

3 minutes read.

Nested Tuple in Python

If you are a Python learner, this page contains all the information that helps you know about nested tuple and how to access it in python. What is a Tuple? Multiple items...

4 minutes read.

Python Call Function

In this article, you will learn about calling a function in Python. But before learning this, we should know about functions in Python.So, let’s get some overview of functions in...

6 minutes read.

Python try catch exception

The try-except proclamation can deal with exceptions. Exceptions might happen when you run a program. Exceptions are blunders that occur during the execution of the program. Python won't educate you regarding...

3 minutes read.

Python Set isdisjoint() method

Python Set isdisjoint() method The set.isdisjoint() method in Python returns a boolean value True if two sets are disjoint sets ( i.e. none of the elements are present in both sets), otherwise it returns...

1 minute read.

Best books for NLP with Python

NLP NLP – Natural language processing Computers were created to make the life of mankind easier. But computers cannot understand human language. Instead, they interact with numbers to perform the tasks allotted...

12 minutes read.

Python hash() function

Python hash() function The hash() function in Python returns the hash value of the object (if it has one).  Syntax hash(object) Parameter obj : This parameter represents the object which we need to convert into hash. Return This...

1 minute read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.

Python EOL (End Of Line)

Introduction An EOL (End Of Line) is defined as a syntax error that indicates that the Python interpreter reached at the end of the line when it tried to scan a...

3 minutes read.

How to print in same line in python

How to Print in the Same Line in Python To print in Python, we use the print () function and the syntax of print () goes like this: print (values, sep =...

3 minutes read.

How to reverse a string in python

How to reverse a string in python A Brief About Strings- The String is a data type in Python that has a sequence of characters. This series of characters are represented in...

4 minutes read.

Python sorted() function

Python sorted() function The sorted() function returns a sorted list of the specified iterable object. Syntax sorted(iterable, *, key=None, reverse=False) Parameter iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc. key:...

1 minute read.

Self in Python

 “self" is neither a keyword nor has a special meaning in Python, but it has a place and a job to do in Object-oriented programming. When we create a class...

6 minutes read.

Python ord() Function

Python ord() Function The ord() function in Python returns the number representing the Unicode code of a specified character. Syntax ord(c) Parameter c: This parameter represents a string or any character. Return This function returns the number...

1 minute read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

Multiple Linear Regression using Python

Linear Regression: Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target...

6 minutes read.

Python List clear() method

Python List clear() method The list.clear () method in Python extends the list by appending all the items from the iterable. Syntax list.clear() Parameter NA Example 1 # Python Program explaining # the list.clear() method week_list = ["Monday", "Tuesday", "Wednesday","Thursday","Friday",...

1 minute read.

Python String capitalize() method

Python String capitalize() method The string.capitalize() method in Python returns a copy of the string with only its first character capitalized. Syntax string.capitalize() Parameter NA Return This function returns a string where the first character is upper...

1 minute read.