Question: Problem 4 a: Calculating best - fit prediction for US Consumption Now, we ll use the same code we used in Problem 2 b (
Problem a: Calculating bestfit prediction for US Consumption
Now, well use the same code we used in Problem b the getproductionneed function to plot the bestfit line for US consumption and a prediction out to years from now.
Write a function called predictneeddata countrycode, predictyears that returns a bestfit prediction line of production necessity data for the given country. It should return it as a dictionary in the form:
"years": "values":
You should use the approach described in the Calculating Bestfit Lines section above. Specifically, your code should:
Similar to Problem a use Problem bs getproductionneed function to get the observed data for each year:
From dataminyear" to datamaxyear"inclusive append the year to a years list and the value from getproductionneed to a values list.Important: polyfit does not accept None values, and so if there are any None values in the data, you need to omit both the value and the year that its for. The example return data below shows that even though AnkhMorpork has production data for that year is not included in the returned data because we do not have consumption data for that country in
Use poly.polyfit to calculate the bestfit line for the data. Remember:
Your file should have import numpy.polynomial as poly at the top. It should already be there for you.The polyfit function takes three parameters. The first two are the x years and y production need for each year values, respectively. The third parameter should be polyfit returns two values, in order: b and then m
Generate the values for the bestfit line and prediction.
Hint: You can do this in a single loop by creating a new list for both the x and y values, looping from the min year dataminyear" to the max year datamaxyear" The body of the loop would append the year to the x list, and mx b to the y list.
Returns a dictionary: years: "values":
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
