In: Statistics and Probability
PYTHON IS3073
1. A group of statisticians at a local college has asked you to create a set of functions that compute the median and mode of a set of numbers, as defined in Section 5.4. Define these functions in a module named stats.py. Also include a function named mean, which computes the average of a set of numbers. Each function should expect a list of numbers as an argument and return a single number. Each function should return 0 if the list is empty. Include a main function that tests the three statistical functions with a given list.
7. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order.
8. A file concordance tracks the unique words in a file and their frequencies. Write a program that displays a concordance for a file. The program should output the unique words and their frequencies in alphabetical order. Variations are to track sequences of two words and their frequencies, or n words and their frequencies.
1) The Python code to compute mean, median and mode of a set of data is given below.
Input the data as an array to each functions.
def mean(data):
total = 0
num_elements =len(data)
if(num_elements == 0):
return(0)
for item in data:
total = total + item
mu = total / (num_elements) # alternately mean = (total / len(data))
return (mu)
def median(data):
num_elements =len(data)
if(num_elements == 0):
return(0)
data.sort()
# determine even or odd
even = True
if len(data)%2 == 1:
even = False
if even:
slice_start = (len(data)//2) - 1
slice_end = (len(data)//2) + 1
me = sum(data[slice_start:slice_end]) / 2
else:
me = data[len(data)//2]
return (me)
def mode(data):
num_elements =len(data)
if(num_elements == 0):
return(0)
hits = []
for item in data:
tally = data.count(item)
values = (tally, item)
# Only add one entry for each number in the set
if values not in hits:
hits.append(values)
hits.sort(reverse=True)
if hits[0][0]>hits[1][0]:
return(hits[0][1])
else:
print("\nThere is not a mode")
return(0)
if __name__ == "__main__":
data = [2,4,6,5,7,8]
print("\nThe mean is:", mean(data))
print("\nThe median is:", median(data))
print("\nThe mode is:", mode(data))
The output is:
The mean is: 5.333333333333333
The median is: 5.5
There is not a mode
The mode is: 0
We are required to answer only one question. Post the remaining questions as another post.