Tuesday, March 7, 2023
HomeSoftware EngineeringThe way to Calculate Variance in Python

The way to Calculate Variance in Python


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

Possibility 1 – Utilizing variance() from Statistics module

import statistics

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

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

Possibility 2 – Utilizing var() from numpy module

import numpy as np

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

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

Possibility 3 – Utilizing sum() and Record Comprehensions

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments