Tuesday, March 7, 2023
HomeSoftware EngineeringLearn how to Calculate Variance in Python

Learn how to Calculate Variance in Python


If you want to calculate variance in Python, then you are able to do the next.

Choice 1 – Utilizing variance() from Statistics module

import statistics

record = [12,14,10,6,23,31]
print("Record : " + str(record))

var = statistics.variance(record)
print("Variance: " + str(var))

Choice 2 – Utilizing var() from numpy module

import numpy as np

arr = [12,43,24,17,32]

print("Array : ", arr)
print("Variance: ", np.var(arr))

Choice 3 – Utilizing sum() and Record Comprehensions

record = [12,43,24,17,32]
common = sum(record) / len(record)
var = sum((x-average)**2 for x in record) / len(record)
print(var)
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments