×

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

How to Define a Function in Python?

What is a Function? In programming, a piece of code that executes a specific task or a group of related operations is known as a function. What does Python Functions Do? If you...

6 minutes read.

To Do GUI Application using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Cx_Oracle Python with Example

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python Set issuperset() method

Python Set issuperset() method The set.issuperset() method in Python returns a boolean value True if all items in the specified set exists in the original set, else it returns False. Syntax set.issuperset (set1) Parameter set- This parameter represents the...

2 minutes read.

Python reversed() Function

Python reversed() Function The reversed() in Python returns a reverse iterator. Syntax reversed(seq) Parameter seq: This parameter represents any iterable object. Return This function returns a reversed iterator object. Example 1 # Python Program describing # the reversed() function ...

1 minute read.

Artificial intelligence mini projects with source code in Python

Project Name: Movie recommendation system A recommendation provides customers with relevant information related to their searches. Before the recommendation system, the most common method of purchasing was to rely on the...

4 minutes read.

Python Classification

Identification and grouping of items or concepts into specified categories this process is known the classification. Data can be separated and sorted in data management according to predetermined criteria for...

6 minutes read.

Python Control Statements

Control statements are under the roof topic of loops and loops in python are defined as iteratively and repeatedly working on source code. Loop control statements are defined as they...

3 minutes read.

Read JSON File in Python

JSON refers to the JavaScript Object Notation. It is a Data format used for representing structured data and it is used to transfer and store data. In JSON format, the...

5 minutes read.

Character Set in Python

A collection of legal characters that will recognize by a programming language known as a Character set. Taking python programming language as an instance. The set of characters supported by the...

6 minutes read.

BOTTLE Python Web Framework

The Bottle is a lightweight WSGI micro web framework for python. It acts like a thin wrapper around a web server where it is distributed as a single file module...

4 minutes read.

Convert string into int in Python

String A string is defined as a series of characters, special characters, and numbers. A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements...

2 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

Python Set update() method

Python Set update() method The set.update() method in Python updates the current set, by adding items from another set. If an element is common in both the sets, only one appearance of this item...

1 minute read.

Text detection using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Scrimba python

Scrimba allows you to study whenever and wherever the topics or concepts you want. It also replaces classroom instruction with interactive screencasts, live events, and student-to-student help. Scrimba is an interactive...

3 minutes read.

Pltpcolor in Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Python Lists vs Tuples

The difference between lists and tuples is one of the most frequently asked questions in an interview related to python language. Lists and Tuples are two of Python’s built-in data...

4 minutes read.

Best Python AI Projects

Artificial consciousness is advancing quickly, from Chabot's to self-driving vehicles. Because of the various advantages and development presented by AI, numerous enterprises have begun searching for AI-fueled applications. Thus, there...

7 minutes read.

Python Packages

A package is a collection of Python modules that share a similar namespace and are generated by putting all of the modules in a single directory with certain special files...

6 minutes read.