在NumPy中从不均匀采样的数据生成均匀采样的数组

J. *_*sen 6 python numpy sample scipy

问题是我想减少绘图和分析的数据量.我正在使用Python和Numpy.数据采样不均匀,因此存在一组时间戳和一组相应的值.我希望它在数据点之间至少有一定的时间.我在这里用Python编写了一个简单的解决方案,其中找到了在样本之间至少有一秒的指标:

import numpy as np

t = np.array([0, 0.1, 0.2, 0.3, 1.0, 2.0, 4.0, 4.1, 4.3, 5.0 ]) # seconds
v = np.array([0, 0.0, 2.0, 2.0, 2.0, 4.0, 4.0, 5.0, 5.0, 5.0 ])

idx = [0]
last_t = t[0]
min_dif = 1.0 # Minimum distance between samples in time
for i in range(1, len(t)):
    if last_t + min_dif <= t[i]:
        last_t = t[i]
        idx.append(i)
Run Code Online (Sandbox Code Playgroud)

如果我们看一下结果:

--> print idx
[0, 4, 5, 6, 9]

--> print t[idx]
[ 0.  1.  2.  4.  5.]
Run Code Online (Sandbox Code Playgroud)

问题是如何更有效地完成这项工作,特别是如果阵列真的很长?是否有一些内置的NumPy或SciPy方法可以做类似的事情?

Pie*_* GM 4

虽然,像@1443118一样,我建议使用pandas,但您可能想尝试使用np.histogram.

min_dif首先,了解您需要的bin 数量(s 的间隔):

>>> bins = np.arange(t[0], t[-1]+min_dif, min_dif) - 1e-12
Run Code Online (Sandbox Code Playgroud)

t[-1]+min_dif是为了确保我们采用最后一点,-1e-12避免将4.0示例的 计数到最后一个 bin 中:这只是一个偏移量,以确保我们关闭右侧的间隔。

>>> (counts, _) = np.histogram(t, bins)
>>> counts
array([4, 1, 1, 0, 3])
>>> counts.cumsum()
array([4, 5, 6, 6, 9])
Run Code Online (Sandbox Code Playgroud)

所以,v[0:4]是你的第一个样本,v[4:5]你的第二个样本……你明白了。