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
test_list = [ 6 , 7 , 3 , 9 , 10 , 15 ]
print ( "The original list is : " + str (test_list))
mean = sum (test_list) / len (test_list)
res = sum ((i - mean) * * 2 for i in test_list) / len (test_list)
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
import statistics
test_list = [ 6 , 7 , 3 , 9 , 10 , 15 ]
print ( "The original list is : " + str (test_list))
res = statistics.variance(test_list)
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
test_list = [ 6 , 7 , 3 , 9 , 10 , 15 ]
print ( "The original list is : " + str (test_list))
res = np.var(test_list)
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: Using pandas
Python3
import pandas as pd
test_list = [ 6 , 7 , 3 , 9 , 10 , 15 ]
print ( "The original list is : " + str (test_list))
res = pd.Series(test_list).var()
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)
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
test_list = [ 6 , 7 , 3 , 9 , 10 , 15 ]
print ( "The original list is : " + str (test_list))
mean = sum (test_list) / len (test_list)
squared_differences = [(i - mean) * * 2 for i in test_list]
variance = sum (squared_differences) / len (test_list)
print ( "The variance of list is : " + str (variance))
|
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)
Last Updated :
17 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...