×

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

Program of Cumulative Sum in Python

Introduction This tutorial, explains what is meant by lists, the cumulative total, several approaches to calculating the cumulative sum of digits in a list, etc. Learn the straightforward Python codes for...

5 minutes read.

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

Python Program for Linear Search

Introduction We employ specific algorithms to efficiently carry out our responsibilities, such as searching for an element in a given data structure. These algorithms fall under the category of searching algorithms....

4 minutes read.

Python Modulo

For basic calculations, Python provides operators. Python supports a broad range of Arithmetic Operators to do arithmetic, as given below: +Addition*Multiplication-Subtraction/Division//Floor division**Exponentiation%Remainder/Modulus As you can see, one of these basic arithmetic operators...

8 minutes read.

Python Program to Generate a Random String

The term "random" refers to a group of information or data that can be accessed in any chronological order. To create random strings, a Python program called random is utilized....

6 minutes read.

Python Debugger

In this tutorial, we will understand what is debugging in general. Then we will realize what the python debugger means and what is the method to perform it. Now, let us...

3 minutes read.

OS Module in Python

What is a module? The Modules are the kind of files that contains python statements and definitions. The module is known by the name of the file followed by the suffix...

8 minutes read.

What is Collaborative Filtering in ML, Python

Introduction Contents recommendation is a useful tactic for almost any specific technology looking to increase interest, but it frequently calls for a lot of user data and perhaps laborious content tagging...

3 minutes read.

Python Set symmetric_difference_update() method

Python Set symmetric_difference_update() method The set.symmetric_difference_update() method in Python updates the original set by removing items that are present in both sets and inserting the other items of the set. Syntax set.symmetric_difference_update(set1) Parameter set- This parameter represents the first...

2 minutes read.

Python String count() method

Python String count() method The string.count() method returns the number of times a specified value appears in the string. Syntax string.count(sub[, start[, end]]) Parameter sub: This parameter represents a substring to be searched. start: This parameter...

1 minute read.

Python Trigonometric Functions

Before jumping right into the functions and how to use them we need to know what do trigonometric functions mean and how many functions does python has. What are Trigonometric functions? Trigonometric...

3 minutes read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.

Working with files in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has an...

5 minutes read.

One Liner If-Else Statements in Python

Before learning one liner if else Python, let us first know what Python is. Python is a broadly helpful, object-oriented and dependable language having a large number of utilizations from...

4 minutes read.

How to Reverse a number in Python?

In Python, conditional statements can be combined with the list() method, slice() method, recursion() method, and other predefined techniques to generate reverse logical functions. The reverse() method is the most...

7 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

Read numpy array in Python

Numpy is a numerical python that deals with multidimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library provides multidimensional array...

9 minutes read.

Python Syntax Error Invalid Syntax

Python is renowned for its simple and direct syntax. However, we might come across some things that Python doesn't allow if we are learning Python for the very first time or if...

14 minutes read.

Python String isdigit() method

Python String isdigit() method The string.isdigit() method returns a boolean value true if all characters in the string are digits else for any other value it returns false. Syntax string.isdigit() Parameter NA Return This method returns a...

2 minutes read.