Skip to main content
 首页 » 编程设计

python之numpy/python中的时间序列平均

2025年05月04日10yxwkf

我的数据由一组时间组成,每秒有 10 个数据点,以及一组与每个时间对应的强度值。因此,举个例子,假设我有:

times = np.arange(0,100,0.1) 
intensities = np.random.rand(len(times)) 

我想看看如果我使用更长的平均时间,数据会是什么样子,所以我想创建一些分箱,比如 1 秒、5 秒和 10 秒,然后对这些新分箱中的强度值进行平均.在 numpy 中执行此操作的最佳方法是什么? (或其他 python 包,但我假设 numpy/scipy 有适合我的东西。)我可以使用 for 循环,但我希望有更好的方法。 谢谢!

请您参考如下方法:

您可以使用 convolve 计算移动平均线,如 stackoverflow 中所述 here .

from pylab import plot, show 
import numpy as np 
 
times = np.arange(0,100,0.1) 
intensities = np.random.rand(len(times)) 
 
def window(size): 
    return np.ones(size)/float(size) 
 
plot(times,intensities,'k.') 
plot(times,np.convolve(intensities,window(10),'same'),'r') 
plot(times,np.convolve(intensities,window(100),'same'),'b') 
show()