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 4a: Calculating best-fit prediction for US Consumption
Now, well use the same code we used in Problem 2b (the get_production_need function) to plot the best-fit line for US consumption and a prediction out to 50 years from now.
Write a function called predict_need(data, country_code, predict_years) that returns a best-fit 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 Best-fit Lines section above. Specifically, your code should:
Similar to Problem 3a, use Problem 2bs get_production_need function to get the observed data for each year:
From data["min_year"] to data["max_year"](inclusive), append the year to a years list and the value from get_production_need 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 Ankh-Morpork has production data for 1960, that year is not included in the returned data because we do not have consumption data for that country in 1960.)
Use poly.polyfit to calculate the best-fit 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 1.polyfit returns two values, in order: b and then m.
Generate the values for the best-fit 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 (data["min_year"]) to the max year (data["max_year"])+51. 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!