简体   繁体   English

如何计算5分钟间隔的平均温度?

[英]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.我有一个练习题,需要一些帮助来计算 5 分钟间隔的平均温度。 Here are the instructions from the problem:以下是问题的说明:

The first column in the.txt file is time in minutes. .txt 文件中的第一列是以分钟为单位的时间。 The second column in the.txt file is temperature in degrees Fahrenheit. .txt 文件中的第二列是以华氏度为单位的温度。

  • Calculate the average temperature in 5 minute intervals by slicing the array(can also be done using a loop).通过对数组进行切片(也可以使用循环来完成),以 5 分钟为间隔计算平均温度。
  • 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.绘制 .txt 文件中的数据和同一图上的平均值,x 轴为时间,y 轴为温度。
  • Plot data1 in red.用红色绘制 data1。
  • 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).用蓝色绘制 data1 的 5 分钟平均值(将它们绘制在平均值的中间,也就是 1 到 5 分钟的平均值,在 3 分钟处绘制)。

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.我主要对计算 5 分钟间隔内的平均温度以及如何绘制这些间隔时间平均值的平均值感到困惑。 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:您可以获得数组arr的 5 个元素的移动平均值,如下所示:

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 F=c×9/5+32

  3. Print the output.打印输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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