Horje
Finding the Variance and Standard Deviation of a list of numbers in Python Code Example
Finding the Variance and Standard Deviation of a list of numbers in Python
# Finding the Variance and Standard Deviation of a list of numbers

def calculate_mean(n):
    s = sum(n)
    N = len(n)
    # Calculate the mean
    mean = s / N 
    return mean 

def find_differences(n):
    #Find the mean
    mean = calculate_mean(n)
    # Find the differences from the mean
    diff = []
    for num in n:
        diff.append(num-mean)
    return diff

def calculate_variance(n):
    diff = find_differences(n)
    squared_diff = []
    # Find the squared differences
    for d in diff:
        squared_diff.append(d**2)
    # Find the variance
    sum_squared_diff = sum(squared_diff)
    variance = sum_squared_diff / len(n)
    return variance

if __name__ == '__main__':
    donations = [100, 60, 70, 900, 100, 200, 500, 500, 503, 600, 1000, 1200]
    variance = calculate_variance(donations)
    print('The variance of the list of numbers is {0}'.format(variance))
    
    std = variance ** 0.5
    print('The standard deviation of the list of numbers is {0}'.format(std))
#src : Doing Math With Python




Python

Related
how to set indian timezone in django Code Example how to set indian timezone in django Code Example
python print list items vertically Code Example python print list items vertically Code Example
how to save bulk create in django Code Example how to save bulk create in django Code Example
get int64 column pandas Code Example get int64 column pandas Code Example
discord.py get user input Code Example discord.py get user input Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7