Question
can someone help me please, any help would be greatly appreciated This question requires you to calculate values using Python functions. There are two approaches
can someone help me please, any help would be greatly appreciated
This question requires you to calculate values using Python functions. There are two approaches you can use.
- add suitable code to the provided file, then run it
- run the provided file first, to load the required function and data into memory, then do the calculation in the interactive Python shell.
Feel free to choose the approach you prefer.
Open the files and inspect the contents of q5.py. The Python list college in q5.py holds 46 yearly values for the number of millions of public college enrolments in the USA (Statista 2019a). The Python list oil in q5.py holds 46 yearly values for a.q5.py imports the Python function median() you used to calculate the median of a list of numbers.
Use this function to find the median of the list college.
In your Solution document give the median, correct to two decimal places. Also provide the Python code you used for calling the median() function and explain how you executed it.
(4 marks)
(here is the Q5 code)
"""
from stats import median from stats import mean from tma02_stats import corr_coef
""" You can use one of two approaches -- add suitable code below and then run this file -- run this file first then do the calculation in the Python interactive shell. """
""" U.S. college enrollment statistics for public colleges from 1971 to 2016 (in millions). """
college = [6.8, 7.07, 7.42, 7.99, 8.83, 8.65, 8.85, 8.79, 9.04, 9.46, 9.65, 9.7, 9.68, 9.48, 9.48, 9.71, 9.97, 10.16, 10.58, 10.84, 11.31, 11.38, 11.19, 11.13, 11.09, 11.13, 11.2, 11.14, 11.38, 11.75, 12.23, 12.75, 12.86, 12.98, 13.02, 13.18, 13.49, 13.97, 14.81, 15.14, 15.12, 14.88, 14.75, 14.65, 14.57, 14.56]
""" Average annual price of a barrel of oil in US dollars from 1971 - 2016. """ oil = [1.7, 1.82, 2.7, 11.0, 10.43, 11.6, 12.5, 12.79, 29.19, 35.52, 34.0, 32.38, 29.04, 28.2
****************************this is the stats code***********************************
import math
def median(alist): """ Calculates the median of a list of numbers. The list must not be empty. """
number_of_values = len(alist) sorted_list = sorted(alist)
# Two cases, depending on whether the number of values is odd or even. quotient = number_of_values // 2 remainder = number_of_values % 2 if (remainder == 1): result = sorted_list[quotient] else: result = (sorted_list[quotient - 1] + sorted_list[quotient]) / 2 return result def test_median(): assert median([2]) == 2 assert median([4, 3]) == 3.5 assert median([3, 1, 8, 4, 7, 6, 4, 2, 5, 9]) == 4.5 assert median([7, 2, 6, 2, 5, 3, 1, 0, 8, 6, 6, 4, 9]) == 5
# Unit test test_median()
def mean(list): """Return mean of list""" sum = 0 count = 0 for item in list: sum = sum + item count = count + 1 return sum / count
def test_mean(): list = [1, 2, 3, 4, 5] assert(mean(list) == 3) # Unit test test_mean()
def corr_coef(list_x, list_y): """ Return correlation between values in list_x and list_y.
Lists must be of equal length. """ x_bar = mean(list_x) y_bar = mean(list_y) sxy = 0 sxx = 0 syy = 0 for index in range(len(list_x)): x = list_x[index] y = list_y[index] sxy = sxy + (x - x_bar) * (y - y_bar) sxx = sxx + (x - x_bar) * (x - x_bar) syy = syy + (y - y_bar) * (y - y_bar) return sxy / math.sqrt(sxx * syy)
def test_corr_coef(): # Data from M140 Unit 9 Example 5 list1 = [78.9, 75.8, 77.3, 74.2, 78.1, 72.8, 77.6, 77.9] list2 = [56.7, 53.1, 56.1, 55.9, 54.1, 48.6, 59.4, 54.0] assert round(corr_coef(list1, list2), 2) == 0.64 # Unit test test_corr_coef()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started