减少用于绘制图表的numpy数组

时间:2012-06-20 07:13:49

标签: python numpy scipy

我想在我的python应用程序中绘制图表,但是源numpy数组太大而不能这样做(大约1'000'000 +)。我想为相邻元素取平均值。第一个想法是用C ++ - 风格:

step = 19000 # every 19 seconds (for example) make new point with neam value
dt = <ordered array with time stamps>
value = <some random data that we want to draw>

index = dt - dt % step
cur = 0
res = []

while cur < len(index):
    next = cur
    while next < len(index) and index[next] == index[cur]:
        next += 1
    res.append(np.mean(value[cur:next]))
    cur = next

但这个解决方案效果很慢。我尝试做this

step = 19000 # every 19 seconds (for example) make new point with neam value
dt = <ordered array with time stamps>
value = <some random data that we want to draw>

index = dt - dt % step
data = np.arange(index[0], index[-1] + 1, step)
res = [value[index == i].mean() for i in data]
pass

此解决方案比第一个慢。这个问题的最佳解决方案是什么?

1 个答案:

答案 0 :(得分:3)

np.histogram可以提供任意二进制数的总和。如果你有时间序列,例如:

import numpy as np

data = np.random.rand(1000)          # Random numbers between 0 and 1
t = np.cumsum(np.random.rand(1000))  # Random time series, from about 1 to 500

然后您可以使用np.histogram

计算5秒间隔内的分箱总和
t_bins = np.arange(0., 500., 5.)       # Or whatever range you want
sums = np.histogram(t, t_bins, weights=data)[0]

如果你想要均值而不是总和,请移除权重并使用bin tallys:

means = sums / np.histogram(t, t_bins)][0]

此方法类似于this answer中的方法。