Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Variance of List

Python | Variance of List

While working with Python, we can have a problem in which we need to find variance of a list cumulative. This problem is common in Data Science domain. Let’s discuss certain ways in which this problem can be solved. 
 

Method #1 : Using loop + formula The simpler manner to approach this problem is to employ the formula for finding variance and perform using loop shorthands. This is the most basic approach to solve this problem. 

Python3




# Python3 code to demonstrate working of
# Variance of List
# using loop + formula
 
# initialize list
test_list = [6, 7, 3, 9, 10, 15]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Variance of List
# using loop + formula
mean = sum(test_list) / len(test_list)
res = sum((i - mean) ** 2 for i in test_list) / len(test_list)
 
# printing result
print("The variance of list is : " + str(res))


Output

The original list is : [6, 7, 3, 9, 10, 15]
The variance of list is : 13.888888888888891

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(1) additional space is not needed

  Method #2 : Using statistics.variance() This task can also be performed using inbuilt function of variance(). 

Python3




# Python3 code to demonstrate working of
# Variance of List
# using statistics.variance
import statistics
 
# initialize list
test_list = [6, 7, 3, 9, 10, 15]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Variance of List
# using statistics.variance
res = statistics.variance(test_list)
 
# printing result
print("The variance of list is : " + str(res))


Output : 

The original list is : [6, 7, 3, 9, 10, 15]
The variance of list is : 13.888888888888891

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(1) constant additional space is required

Method #3 : Using numpy.var()

Note: Install numpy module using command “pip install numpy”

This task can also be performed using numpy library which provides an inbuilt function numpy.var() to calculate variance of a list.

Python3




import numpy as np
 
# initialize list
test_list = [6, 7, 3, 9, 10, 15]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Variance of List
# using numpy.var()
res = np.var(test_list)
 
# printing result
print("The variance of list is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output :
The original list is : [6, 7, 3, 9, 10, 15]
The variance of list is : 13.888888888888891

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(1) constant additional space is required

Method: Using pandas

Python3




import pandas as pd
 
# initialize list
test_list = [6, 7, 3, 9, 10, 15]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = pd.Series(test_list).var()
# printing result
print("The variance of list is : " + str(res))
#This code is contributed Vinay Pinjala.


Output :
The original list is : [6, 7, 3, 9, 10, 15]
The variance of list is : 13.888888888888891

Time Complexity:O(n)

Auxiliary Space: O(n)

Method: Using List comprehension:

First, we define the test list test_list with some sample numbers.
Then, we print the original list using the print() function and string concatenation.
Next, we calculate the mean of the list by adding up all the elements in the list using the sum() function and dividing by the length of the list using the len() function. We save this mean in a variable called mean.
Then, we calculate the squared differences between each element in the list and the mean using a list comprehension. This involves iterating over the list of numbers and subtracting the mean from each number, squaring the result, and storing it in a new list called squared_differences.
Finally, we calculate the variance of the list by summing the squared differences and dividing by the length of the list. We print the variance using the print() function and string concatenation.
 

Python3




# Define the test list
test_list = [6, 7, 3, 9, 10, 15]
# Print the original list
print("The original list is : " + str(test_list))
# Calculate the mean of the list
mean = sum(test_list) / len(test_list)
# Calculate the squared differences between each element in the list and the mean
squared_differences = [(i - mean) ** 2 for i in test_list]
# Calculate the variance of the list by summing the squared differences and dividing by the length of the list
variance = sum(squared_differences) / len(test_list)
# Print the variance of the list
print("The variance of list is : " + str(variance))
#This code is contributed by Jyothi pinjala.


Output

The original list is : [6, 7, 3, 9, 10, 15]
The variance of list is : 13.888888888888891

Time Complexity: O(n)

Auxiliary Space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments