Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using python Before calculating the moving average you will need to read the temperature data into a list so your program can read the same

Using python

image text in transcribed

Before calculating the moving average you will need to read the temperature data into a list so your program can read the same value over and over when sliding the window for the moving average. Change the loop in your program that reads the year and temperature data from the file into two lists: a list holding the years and a list holding the floating point temperatures. These lists will make it possible to repeatedly access the data when computing the average. Moving average calculation The moving average at position i in the list is calculated as the sum of the temperature values from position i-k to position i+k divided by the total width of the window (2*k + 1). Below is an example of the calculation for k= 5 The boxed below illustrate a window at position i with width k=5 in the temps list: i-5 i-4 i- 3 i-2 i-1 i i+1 i+ 2 i+3 i+4 i+5 ave = (temps[i-k] + ...temps[i-1] + temps[i] + temps[i+1]... + temps[i+k])/(2*k+1) year = years[i] Your calculation will not look exactly like this. You will need to implement the sum using a nested loop or using the sum() function on a slice of the list. Also note is that this calculation is not valid near the ends of the list. You should write a loop that only calculates the average for valid years. In other words if temps[i-k] can not have an index of less than 0, then i cannot be less than k. A similar constraint holds for the other end of the list. Write a loop to compute the average temperature for each valid year. Your loop should print the year and the average separated by a comma. Format the printed temperature to four decimal places using the format method. Here is an example of the output your program should produce when the user chooses to average over k=60 years; there are not too many rows since there are only 10 years in the middle of the century when there are 60 years before and 60 years after over which to average. Temperature anomaly filename: NorCal-1880-2018.csv Enter an integer between 0 and 60:60 1940,-0.2331 1941,-0.2169 1942,-0.2150 1943,-0.2228 1944,-0.2107 Before calculating the moving average you will need to read the temperature data into a list so your program can read the same value over and over when sliding the window for the moving average. Change the loop in your program that reads the year and temperature data from the file into two lists: a list holding the years and a list holding the floating point temperatures. These lists will make it possible to repeatedly access the data when computing the average. Moving average calculation The moving average at position i in the list is calculated as the sum of the temperature values from position i-k to position i+k divided by the total width of the window (2*k + 1). Below is an example of the calculation for k= 5 The boxed below illustrate a window at position i with width k=5 in the temps list: i-5 i-4 i- 3 i-2 i-1 i i+1 i+ 2 i+3 i+4 i+5 ave = (temps[i-k] + ...temps[i-1] + temps[i] + temps[i+1]... + temps[i+k])/(2*k+1) year = years[i] Your calculation will not look exactly like this. You will need to implement the sum using a nested loop or using the sum() function on a slice of the list. Also note is that this calculation is not valid near the ends of the list. You should write a loop that only calculates the average for valid years. In other words if temps[i-k] can not have an index of less than 0, then i cannot be less than k. A similar constraint holds for the other end of the list. Write a loop to compute the average temperature for each valid year. Your loop should print the year and the average separated by a comma. Format the printed temperature to four decimal places using the format method. Here is an example of the output your program should produce when the user chooses to average over k=60 years; there are not too many rows since there are only 10 years in the middle of the century when there are 60 years before and 60 years after over which to average. Temperature anomaly filename: NorCal-1880-2018.csv Enter an integer between 0 and 60:60 1940,-0.2331 1941,-0.2169 1942,-0.2150 1943,-0.2228 1944,-0.2107

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

How does the 10-K differ from the 10-Q?

Answered: 1 week ago