我想在我的 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
但这个解决方案工作得很慢。我试着这样做:
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
此解决方案比第一个解决方案慢。这个问题的最佳解决方案是什么?