Question: Need this in python, a screenshot of the code would be greatly appreciated! Part 4 (15 points) Moving average part 1: first window Start a
Need this in python, a screenshot of the code would be greatly appreciated!


Part 4 (15 points) Moving average part 1: first window Start a new program first_ave.py from your working version of temp_list.py. Comment out the line that prints out the list of temperatures. For the moving average, we will let the user enter an integer k, then, for each year in our data file we will calculate the average of the k years before, the year itself, and the k years after that year. We can visualize this process as a window that slides across our temperature data. In the figure below the window is represented by the curly brackets. The window is centered on an index in the temperature list. We are interested in calculating the average temperature for the values in the window and the year on which the window is centered. The window slides across the temperature list, and at each new position we calculate the average and the year. first window sliding window at position i size = k index = k year = 1880+k size = k index = i year = 1880+index temps list 0 K-1 k k+ 1 k+k i-k i-1 i i+ 1 i+k len(temps)-1 Before starting the moving average calculation, you will first get the window size parameter k from the user. You may assume that the user will always enter a valid integer between 0 and 60. For example: Enter window size:20 You can see in the figure that the moving average calculation is not valid for years near the ends of the list. We must have at least k years before and k years after the year we are computing the average for. In the first part we will focus on just calculating the average and the year for the first window. For the first valid year in your temperature list, you will calculate the average of the k years before, the year itself, and the k years after. The easiest way to do this is using a list slice. The following three statements compute and print the year and the moving average for the first valid index in a list of temperatures called temps. index = k year = 1880 + index ave = sum(temps [0:k+k+1]) / (2*k+1) You should be able to insert these statements at the end of your program to calculate the year and the average. The output of your program for a window size of 20 will eventually look exactly like this: Temperature anomaly filename : SacramentoTemps .csv Enter window size:20 1900,-0.4171 Once you get the calculations for year and average working, format the output so that the values are separated by a comma and the average temperature is printed with exactly four decimal places using the format string method. Submit the completed program to Gradescope as first_ave.py
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
