numpy/scipy 中的矢量化索引/切片?
我有一个数组 A,还有一个切片索引 (s,t) 列表,我们将这个列表称为 L。
我想找到 A[s1:t1]、A[s2:t2] 的 85 个百分位数...
有没有办法在 numpy 中向量化这些操作?
ans = []
for (s,t) in L:
ans.append( numpy.percentile( A[s:t], 85) );
看起来很麻烦。
多谢!
PS:可以安全地假设 s1 < s2 .... t1 < t2 .....这实际上只是一个滑动窗口百分位数问题。
I have an array A, and I have a list of slicing indices (s,t), let's called this list L.
I want to find the 85 percentiles of A[s1:t1], A[s2:t2] ...
Is there a way to vectorize these operations in numpy?
ans = []
for (s,t) in L:
ans.append( numpy.percentile( A[s:t], 85) );
looks cumbersome.
Thanks a lot!
PS: it's safe to assume s1 < s2 .... t1 < t2 ..... This is really just a sliding window percentile problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑到您正在处理不均匀的间隔(即切片的大小不同),不,没有办法让 numpy 在单个函数调用中完成它。
如果它是统一的切片大小,那么您可以使用各种技巧来做到这一点,正如@eat 评论的那样。
然而,列表理解有什么问题呢?它与上面的循环完全相同,但如果您担心的话,它看起来“更干净”。
Given that you're dealing with a non-uniform interval (i.e. the slices aren't the same size), no, there's no way to have numpy do it in a single function call.
If it was a uniform slice size, then you could do so with various tricks, as @eat commented.
However, what's wrong with a list comprehension? It's exactly equivalent to your loop above, but it looks "cleaner" if that's what you're worried about.