Q:

Python program to find the variance

0

Variance tells us about the divergence and the inconsistency of the sample. So in this python article, we are going to build a function.

Mathematically we define it as:

variance in Python

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

So here is the code:

def variance(X):
    mean = sum(X)/len(X)
    tot = 0.0
    for x in X:
        tot = tot + (x - mean)**2
    return tot/len(X)

# main code
#  a simple data-set 
sample = [1, 2, 3, 4, 5] 
print("variance of the sample is: ", variance(sample))

sample = [1, 2, 3, -4, -5] 
print("variance of the sample is: ", variance(sample))

sample = [10, -20, 30, -40, 50] 
print("variance of the sample is: ", variance(sample))

Output:

ariance of the sample is:  2.0
variance of the sample is:  10.64
variance of the sample is:  1064.0

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now