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.
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.
Convert the temperature celsius to by using the following formula.
F=c×9/5+32
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.