python - Writing a standard deviation function -


i have dictionary of words keys , ints value. outputs such:

print (word_ratings_dict) {'hate': [1, 2, 2, 1, 1, 3, 0, 2, 3, 2, 0, 4, 1, 1], 'joy': [3, 4, 3, 3, 2, 4, 1]} 

for each key word in dictionary, need calculate standard deviation without using statistics module.

heres have far:

def menu_validate(prompt, min_val, max_val):     """ produces prompt, gets input, validates input , returns value. """     while true:         try:             menu = int(input(prompt))             if menu >= min_val , menu <= max_val:                 return menu                 break             elif menu.lower == "quit" or menu.lower == "q":                 quit()             print("you must enter number value {} {}.".format(min_val, max_val))         except valueerror:             print("you must enter number value {} {}.".format(min_val, max_val))  def open_file(prompt):     """ opens file """     while true:         try:             file_name = str(input(prompt))             if ".txt" in file_name:                 input_file = open(file_name, 'r')                 return input_file             else:                 input_file = open(file_name+".txt", 'r')                 return input_file         except filenotfounderror:             print("you must enter valid file name. make sure file open in programs root folder.")  def make_list(file):     lst = []     line in file:         lst2 = line.split(' ')         del lst2[-1]         lst.append(lst2)     return lst  def rating_list(lst):     '''iterates through list of lists , appends first value in each list second list'''     rating_list = []     list in lst:         rating_list.append(list[0])     return rating_list  def word_cnt(lst, word : str):     cnt = 0     list in lst:         word in list:             cnt += 1     return cnt  def words_list(file):     lst = []     word in file:         lst.append(word)     return lst  def word_rating(word, ratings_lst):     '''finds ratings word , appends them dictionary of words'''     lst = []     line in ratings_lst:         line = line.split()         if word in line:             rating = line[0]             lst.append(int(rating))     return lst cnt_list = [] while true:     menu = menu_validate("1. sentiment words in file? \nq. quit \n", 1, 1)     if menu == true:         ratings_file = open("sample.txt")         ratings_list = make_list(ratings_file)         word_ratings_dict = {}         word_avg_dict = {}         std_dev_dict = {}         word_file = open_file("enter name of file words score \n")         word_list = words_list(word_file)           word in word_list:             #counts words             cnt = word_cnt(ratings_list, word)              cnt_dict[word] = cnt              word_ratings_dict[word] = word_rating(word, ratings_list)              total_rating = 0              in range (0, cnt):                 total_rating += word_ratings_dict[word][i]              word_avg_dict[word] = total_rating/cnt              std_dev_dict[word] =  

these job nicely:

def mean(data):     return float(sum(data) / len(data))  def variance(data):     mu = mean(data)     return mean([(x - mu) ** 2 x in data])  def stddev(data):     return sqrt(variance(data)) 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -