- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一些大型数组(约 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.argmin
和 numpy.argmax
函数中,它们比 numpy.min
和 numpy 慢一点.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/
我对编码还比较陌生,但并非完全没有经验。处理有关金融计算器的学校作业。如果你们中的任何人可以查看我的代码以了解不良做法/可能的改进等,那就太好了。 我确实添加了一个“动画”启动(有很多 printf
小目标Trick 论文链接: https://paperswithcode.com/paper/slicing-aided-hyper-inference-and-fine-tuning 代码链接:h
if (firstPositionCpc && (firstPosition > 0 && firstPositionCpc 0 && topOfPageCpc 0 && firstPageCpc
我有 2 个表:“packages”和“items”。 “packages”有以下列:pack_id | item_id “items”有以下列......:item_id |输入 一个包可以有多个
我目前有一个 Pandas Dataframe,我在其中执行列之间的比较。我发现一种情况,在进行比较时存在空列,由于某种原因比较返回 else 值。我添加了一个额外的语句来将其清理为空。看看我是否可以
我正在处理一个查询,通过首先舍入它们的主要日期时间键来连接一个数据库中的多个表。数据库包含来自 openhab 的性能数据,每个表只有一个名为 Time 的主日期时间行和一个名为 Value 的值行。
问候 我有一个程序创建一个类的多个实例,在所有实例上运行相同的长时间运行的 Update 方法并等待完成。我从 this question 开始关注 Kev 的方法将更新添加到 ThreadPool.
我想在下学期的类(class)中取得领先,所以我制作了这个基本版本的 Blackjack 来开始理解 C 的基础知识,我希望您有任何想法可以帮助我更好地理解 C 和其正常的编码实践。 C 中的很多东西
我有一个要求,比如: 给定一个数组,其中包含随机数。需要输出元素出现的次数,有自带解决方案: var myArr = [3,2,1,2,3,1,4,5,4,6,7,7,9,1,123,0,123];
这是我的数据库项目。 表user_ select id, name from user_; id | name ----+---------- 1 | bartek 2 | bartek
我已经完成了一个小批量脚本来调整(动态)一些图像的大小: for a in *.{png,PNG,jpg,JPG,jpeg,JPEG,bmp,BMP} ; do convert "$a" -resiz
是否有更 pythonic 的方法来执行以下代码?我想在一行中完成 parsed_rows 是一个可以返回大小为 3 或 None 的元组的函数。 parsed_rows = [ parse_row(
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improv
下面的代码完成了我想要的,但还有其他更像 python 风格的方式吗? 文件格式: key1:value1,key2:value2,... key21:value21,key22:value22,..
如果两个英文单词只包含相同的字母,则它们是相似的。例如,food 和 good 不相似,但 dog 和 good 相似。 (如果A与B相似,则A中的所有字母都包含在B中,B中的所有字母都包含在A中。)
我有以下结构来表示二叉树: typedef struct node *pnode; typedef struct node { int val; pnode left; pnode
我有一个区域,它由受约束的 delaunay 三角剖分表示。我正在解决在两点之间寻找路径的问题。我正在使用 Marcelo Kallmann 提供的论文作为解决此问题的引用点。然而,而不是使用 Kal
如果我需要检查文本(字符串)中是否存在单词 A 或单词 B,如果我这样做会有性能差异: if(text.contains(wordA) || text.contains(wordB)) 要使用一些正则
Adjust To 我有上面这个简单的页面,上面有一个标签和一个文本框。我想在文本框中输入文本。 对我有帮助的 XPATH 是 //*[contains(tex
以下伪代码的elisp代码 if "the emacs version is less than 23.1.x" do something else something-else 写成 (if
我是一名优秀的程序员,十分优秀!