我希望在我的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
这个解决方案比第一个慢。那么对于这个问题,最好的解决方案是什么?