×

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

Append key Value to Dictionary in Python

The Python dictionary is one of the built-in data types. Elements of dictionaries are key-value pairs. In Python, there are numerous ways to add dictionaries. Let's examine some of the...

9 minutes read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

Io stringio Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

7 minutes 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.

Python Functionalities of Pillow Module

This instructional exercise is about "Pillow" library, one of the significant libraries of python for picture control. Pillow library of python, is an easy to use and also open source...

14 minutes read.

How To Install Python In Ubuntu

How To Install Python In Ubuntu Ubuntu is free and open-source software and it is an essential part of the Linux distribution. It is a popular operating system developed by Canonical. If we...

3 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.

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

Python 2.7 data structures

The rundown information type has a few additional techniques. Here is every one of the strategies for listing objects: list.append(x): Add a thing to the furthest limit of the rundown; comparable...

9 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

Python md5() function

Python md5() function The md5() function in PHP calculates the md5 hash of a string. Syntax md5 ( string $str [, bool $raw_output] ) Parameter str(required)- This parameter specifies the string. raw_output(optional)- This parameter specifies a hex or binary output format: TRUE - Raw 16 character binary...

1 minute read.

Python program to find the greatest among two numbers

Python program to find the greatest among two numbers We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them....

2 minutes read.

Read Text files in Python

In Python, there are many ways to read text files. Before going into the detailed structure of reading a text file, let us understand how reading text files takes place...

4 minutes read.

What are the Purposes of Python?

Python is a high-level programming language that is simple to use and easy to write, and Python is a beginner-friendly programming language. A beginner often prefers to start with Python...

6 minutes read.

Filter List in Python

Python: Python is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a user-friendly programming...

3 minutes read.

Data Class in Python

Introduction In Python 3.7, the dataclass modules are introduced as a practical tool for creating organized classes designed specifically for data storage. These classes contain specific attributes and capabilities to deal...

6 minutes read.

OSError 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...

4 minutes read.

Pointers in Python

In this tutorial, we will study what pointers are and if they have any utility in python. Now, let us understand what pointers are Pointers Pointers are special variables used to store the...

3 minutes read.

Python any() Function

Python any() Function The any() function in Python returns a boolean value ‘True’ if any element of the iterable is true or if the iterable is empty, else it returns False. Syntax any(iterable) Parameter Iterable: An iterable object (list, tuple, dictionary) Return This...

1 minute read.