简体   繁体   English

numpy / python中的时间序列平均

[英]Time-series averaging in numpy/python

I have data that consists of an array of times, with 10 data points each second, and an array of intensity values corresponding to each time. 我的数据由一个时间数组组成,每秒包含10个数据点,每个时间对应一个强度值数组。 So, for an example let's say that I have: 因此,举个例子,我有:

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

I want to see what the data will look like if I use a longer averaging time, so I want to create some bins, of, say 1 second, 5 seconds, and 10 seconds and average the intensity values in those new bins. 我想看看如果我使用更长的平均时间,数据将是什么样子,所以我想创建一些仓(例如1秒,5秒和10秒),并对这些新仓中的强度值进行平均。 What is the best way to do this in numpy? 在numpy中执行此操作的最佳方法是什么? (Or other python package, but I'm assuming numpy/scipy has something for me.) I could use a for loop, but I'm hoping there is a better way. (或其他python包,但我假设numpy / scipy有适合我的东西。)我可以使用for循环,但我希望有更好的方法。 Thanks! 谢谢!

You can calculate moving averages using convolve as mentioned on stackoverflow here . 您可以按照此处 stackoverflow所述使用convolve计算移动平均值。

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()

在此处输入图片说明

You could reshape the data to group it into groups of 10, 50, or 100. Then call the mean(axis=-1) method to take the average over the last axis (the axis of size 10, 50, or 100): 您可以对数据进行整形以将其分组为10、50或100的组。然后调用mean(axis=-1)方法以获取最后一个轴(大小为10、50或100的轴mean(axis=-1)的平均值:

With this setup: 使用此设置:

In [10]: import numpy as np

In [11]: times = np.linspace(0,100,1000)

In [12]: intensities = np.random.rand(len(times))

Here is the means of every 10 values: 这是每10个值的平均值:

In [13]: intensities.reshape(-1,10).mean(axis=-1)
Out[13]: <output omitted due to length>

means of every 50 values: 每50个值的平均值:

In [14]: intensities.reshape(-1,50).mean(axis=-1)
Out[14]: <output omitted due to length>

means of every 100 values: 每100个值的平均值:

In [15]: intensities.reshape(-1,100).mean(axis=-1)
Out[15]: 
array([ 0.50969463,  0.5095131 ,  0.52503152,  0.49567742,  0.52701341,
        0.53584475,  0.54808964,  0.47564486,  0.490907  ,  0.50293636])

arr.reshape(-1, 10) tells NumPy to reshape the array arr to have a shape with size 10 in the last axis. arr.reshape(-1, 10)告诉NumPy arr.reshape(-1, 10)数组arr的形状,使其最后一个轴的大小为10。 The -1 tells NumPy to give the first axis whatever size is necessary to fill the array. -1告诉NumPy给第一个轴赋予填充数组所需的任何大小。

Note that using reshape in this way requires that len(intensities) is evenly divisible by the size (eg 10, 50, 100) that you want to group by. 请注意,以这种方式使用reshape要求len(intensities)被要分组的大小(例如10、50、100 len(intensities)均匀地整除。

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

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