简体   繁体   中英

How to calculate the average temperature in 5 minute intervals?

I have a practice problem, and need some help calculating the average temperature for 5 minute intervals. Here are the instructions from the problem:

The first column in the.txt file is time in minutes. The second column in the.txt file is temperature in degrees Fahrenheit.

  • Calculate the average temperature in 5 minute intervals by slicing the array(can also be done using a loop).
  • Plot the data from the.txt file and the averages on the same plot with time on the x-axis and temperature on the y-axis.
  • Plot data1 in red.
  • Plot the 5 minute averages of data1 in blue (plot them in the middle of the average, aka for the average of 1 to 5 min plot it at 3 min).

I'm mainly confused on calculating the average temperature in a 5 minute interval and then on how to graph those averages for the time average of the interval. I'll also include the code that I have below, but it is missing those two parts of the problem.

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt("sample1.txt")    
middle_of_interval = np.average()

plt.plot(data[:,0], data[:,1], "r") 
plt.title("Temperature vs. Time")  
plt.xlabel("Time (minutes)")        
plt.ylabel("Temperature (F)")       
plt.xticks(np.arange(0, 30, 1))     
plt.yticks(np.arange(23, 27, 0.5)) 

It's always a good idea to avoid looping over arrays; better to stick to slicing and other tricks.

You can get a moving average over 5 elements of an array arr like so:

avg = (arr[:-4] + arr[1:-3] + arr[2:-2] + arr[3:-1] + arr[4:]) / 5

You could also convolve a small boxcar with the time series, like so:

boxcar = np.ones(5) / 5
avg = np.convolve(arr, boxcar, mode='valid')

Do the same thing to the time series to get the average time of each measurement.

  1. Prompt the user to enter the temperature in Fahrenheit.
  2. Convert the temperature celsius to by using the following formula.

    F=c×9/5+32

  3. Print the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM