Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
Comment

PREVIOUS NEXT
Code Example
Python :: scikit learn split data set 
Python :: py insert char at index 
Python :: python timedelta 
Python :: how to record pyttsx3 file using python 
Python :: print random word py 
Python :: how to set datetime format in python 
Python :: python version check 
Python :: pygame mouse pos 
Python :: hide password input tkinter 
Python :: hot reloading flask 
Python :: pandas string does not contain 
Python :: python strip newline from string 
Python :: panda datetime ymd to dmy 
Python :: how to convert input to uppercase in python 
Python :: jupyter notebook change default directory 
Python :: check django version 
Python :: convert set to list python time complexity 
Python :: how to get the mouse input in pygame 
Python :: python loop break on keypress 
Python :: python find word in list 
Python :: how to remove all zeros from a list in python 
Python :: pandas plot histogram 
Python :: https flask 
Python :: sort list of dictionaries python 
Python :: python read column data from text file 
Python :: mount drive google colab 
Python :: zlib decompress python 
Python :: Tkinter canvas draggable 
Python :: json indent options python 
Python :: decode html python 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =