gpt4 book ai didi

python - 改进最小/最大下采样

转载 作者:太空狗 更新时间:2023-10-29 21:41:21 25 4
gpt4 key购买 nike

我有一些大型数组(约 1 亿个点)需要进行交互式绘图。我目前正在使用 Matplotlib。按原样绘制数组变得非常慢并且是一种浪费,因为无论如何您都无法可视化那么多点。

所以我创建了一个最小/最大抽取函数,该函数与轴的“xlim_changed”回调相关联。我采用最小/最大方法,因为数据包含我不想通过单步执行数据而错过的快速峰值。有更多包装器裁剪到 x 限制,并在某些条件下跳过处理,但相关部分如下:

def min_max_downsample(x,y,num_bins):
""" Break the data into num_bins and returns min/max for each bin"""
pts_per_bin = x.size // num_bins

#Create temp to hold the reshaped & slightly cropped y
y_temp = y[:num_bins*pts_per_bin].reshape((num_bins, pts_per_bin))
y_out = np.empty((num_bins,2))
#Take the min/max by rows.
y_out[:,0] = y_temp.max(axis=1)
y_out[:,1] = y_temp.min(axis=1)
y_out = y_out.ravel()

#This duplicates the x-value for each min/max y-pair
x_out = np.empty((num_bins,2))
x_out[:] = x[:num_bins*pts_per_bin:pts_per_bin,np.newaxis]
x_out = x_out.ravel()
return x_out, y_out

这工作得很好并且足够快(在 1e8 点和 2k bin 上大约 80 毫秒)。几乎没有延迟,因为它会定期重新计算和更新线路的 x 和 y 数据。

但是,我唯一的提示是 x 数据。此代码复制每个 bin 左边缘的 x 值,并且不返回 y 最小/最大对的真实 x 位置。我通常将 bin 的数量设置为轴像素宽度的两倍。所以你真的看不出区别,因为垃圾箱太小了……但我知道它在那里……这让我很烦。

因此请尝试第 2 次,它会返回每个最小/最大对的实际 x 值。然而,它慢了大约 5 倍。

def min_max_downsample_v2(x,y,num_bins):
pts_per_bin = x.size // num_bins
#Create temp to hold the reshaped & slightly cropped y
y_temp = y[:num_bins*pts_per_bin].reshape((num_bins, pts_per_bin))
#use argmax/min to get column locations
cc_max = y_temp.argmax(axis=1)
cc_min = y_temp.argmin(axis=1)
rr = np.arange(0,num_bins)
#compute the flat index to where these are
flat_max = cc_max + rr*pts_per_bin
flat_min = cc_min + rr*pts_per_bin
#Create a boolean mask of these locations
mm_mask = np.full((x.size,), False)
mm_mask[flat_max] = True
mm_mask[flat_min] = True
x_out = x[mm_mask]
y_out = y[mm_mask]
return x_out, y_out

这在我的机器上大约需要 400 多毫秒,这变得非常明显。所以我的问题基本上是有没有办法更快地提供相同的结果?瓶颈主要在 numpy.argminnumpy.argmax 函数中,它们比 numpy.minnumpy 慢一点.max.

答案可能是只使用版本 #1,因为它在视觉上并不重要。或者也许尝试加快它的速度,比如 cython(我从未使用过)。

仅供引用,在 Windows 上使用 Python 3.6.4 ...示例用法如下:

x_big = np.linspace(0,10,100000000)
y_big = np.cos(x_big )
x_small, y_small = min_max_downsample(x_big ,y_big ,2000) #Fast but not exactly correct.
x_small, y_small = min_max_downsample_v2(x_big ,y_big ,2000) #correct but not exactly fast.

最佳答案

我设法通过使用 arg(min|max) 的输出直接索引数据数组来提高性能。这是以额外调用 np.sort 为代价的,但是要排序的轴只有两个元素(最小/最大索引)并且整个数组相当小(bin 数量):

def min_max_downsample_v3(x, y, num_bins):
pts_per_bin = x.size // num_bins

x_view = x[:pts_per_bin*num_bins].reshape(num_bins, pts_per_bin)
y_view = y[:pts_per_bin*num_bins].reshape(num_bins, pts_per_bin)
i_min = np.argmin(y_view, axis=1)
i_max = np.argmax(y_view, axis=1)

r_index = np.repeat(np.arange(num_bins), 2)
c_index = np.sort(np.stack((i_min, i_max), axis=1)).ravel()

return x_view[r_index, c_index], y_view[r_index, c_index]

我检查了你的例子的时间,我得到:

  • min_max_downsample_v1:110 毫秒 ± 5 毫秒
  • min_max_downsample_v2:240 毫秒 ± 8.01 毫秒
  • min_max_downsample_v3:164 毫秒 ± 1.23 毫秒

我还检查了在调用 arg(min|max) 之后直接返回,结果同样是 164 毫秒,也就是说,在那之后就没有真正的开销了。

关于python - 改进最小/最大下采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54449631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com