Home > OS >  How to calculate the mean and SD from multiple lists in a dictionary and sort them into a new dictio
How to calculate the mean and SD from multiple lists in a dictionary and sort them into a new dictio

Time:12-19

I have a dictionary containing 5 keys, each key has 10 values assigned to it in the form of a list:

{'a': [5, 6, 3, 1, 3, 2, 5, 8, 6, 7], 
 'b': [3, 5, 2, 7, 0, 2, 10, 4, 3, 4], 
 'c': [9, 7, 11, 10, 8, 9, 7, 10, 7, 9], 
 'd': [6, 4, 5, 7, 6, 8, 5, 6, 5, 7], 
 'e': [2, 5, 1, 4, 2, 3, 4, 2, 5, 1]}

I also have an empty dictionary that I have set up that I want to store the mean and standard deviation for each list in the original dictionary:

{'a_analysis': [],
 'b_analysis': [],
 'c_analysis': [],
 'd_analysis': [],
 'e_analysis': []}

I have this code so far:

for key in original_dictionary:   #for each key in the dictionary
   for value in original_dictionary:   #iterate through the values in each key
      sum =  value   #add the value to a sum variable
   mean = sum / len(orignal_dictionary[key])   #get the mean by dividing the sum by the len of each key
   #here I want to return the mean value to the respective key in the new dictionary 
   sd =    #then I need to get a value for the standard deviation here 
   #and also return it to the respective key in the new dictionary

Any help would be greatly appreciated.

CodePudding user response:

Here is a solution, I hope you like this:

def standard_deviation(numbers):
    mean = sum(numbers) / len(numbers)
    result = (sum( [((x - mean) ** 2) for x in numbers] ) / len(numbers))**0.5
    return result

input ={
'a': [5, 6, 3, 1, 3, 2, 5, 8, 6, 7],
'b': [3, 5, 2, 7, 0, 2, 10, 4, 3, 4],
'c': [9, 7, 11, 10, 8, 9, 7, 10, 7, 9],
'd': [6, 4, 5, 7, 6, 8, 5, 6, 5, 7],
'e': [2, 5, 1, 4, 2, 3, 4, 2, 5, 1]
}
output = {}
for key in input.keys():
    output[key "_analysis"] = {"mean": sum(input[key])/len(input[key]),
                               "standard deviation":standard_deviation(input[key]),
                               }

and Here is the output:

{'a_analysis': {'mean': 4.6, 'standard deviation': 2.1540659228538015},
 'b_analysis': {'mean': 4.0, 'standard deviation': 2.6832815729997477},
 'c_analysis': {'mean': 8.7, 'standard deviation': 1.345362404707371},
 'd_analysis': {'mean': 5.9, 'standard deviation': 1.1357816691600546},
 'e_analysis': {'mean': 2.9, 'standard deviation': 1.445683229480096}}

CodePudding user response:

You can also use pandas for this:

import pandas as pd
out = pd.DataFrame(your_dict).agg(['mean', 'std']).to_dict()

Output:

{'a': {'mean': 4.6, 'std': 2.2705848487901865},
 'b': {'mean': 4.0, 'std': 2.8284271247461903},
 'c': {'mean': 8.7, 'std': 1.4181364924121764},
 'd': {'mean': 5.9, 'std': 1.1972189997378646},
 'e': {'mean': 2.9, 'std': 1.5238839267549948}}

CodePudding user response:

This should work. Hope I helped you :)

import statistics

direc = {'a': [5, 6, 3, 1, 3, 2, 5, 8, 6, 7], 
 'b': [3, 5, 2, 7, 0, 2, 10, 4, 3, 4], 
 'c': [9, 7, 11, 10, 8, 9, 7, 10, 7, 9], 
 'd': [6, 4, 5, 7, 6, 8, 5, 6, 5, 7], 
 'e': [2, 5, 1, 4, 2, 3, 4, 2, 5, 1]}

analyse = {'a_analysis': [],
 'b_analysis': [],
 'c_analysis': [],
 'd_analysis': [],
 'e_analysis': []}

for key in direc.items():
        sum = 0
        lenght = len(key[1])
        for i in key[1]:
            sum = sum   int(i)
        mean = sum / lenght
        string = str(key[0])   '_analysis'
        sd = statistics.stdev(key[1])
        analyse[string] = [mean, sd]

print(analyse)
  • Related