Numpy 向量化算法查找第一个大于当前元素的未来元素
我有一个时间序列 A。我想生成另一个时间序列 B,使得
B[i] = j,其中 j 是第一个大于 i 的索引,使得 A[j] > 。人工智能]。
有没有一种快速的方法可以在 numpy 中做到这一点?
谢谢。
[编辑]:最好仅使用 O(n) 的空间。
I have a time series A. I want to generate another time series B, such that
B[i] = j, where j is the first index greater than i such that A[j] > A[i].
is there a fast way of doing this in numpy?
Thanks.
[EDITED]: preferably use only O(n) of space.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
未经充分测试,使用风险自负。
内存较低的版本:
更快的版本:
甚至更快的版本:
这里使用的一些想法归功于 cyborg。
算法差异
cyborg 根据输入的数据显示出各种算法之间的显着差异。我从运行的算法中收集了一些统计数据,看看我是否能弄清楚发生了什么。
随机数据:
由于列表非常短,因此上升算法大多会退化为线性搜索。它确实清除了将来无法使用的上升,因此它仍然明显优于线性搜索。
振荡:
振荡往往会使不同的部分分开得更远。这自然会阻碍线性搜索算法。两种“更智能”的算法都必须跟踪附加数据。我的算法会大幅衰减,因为它每次都会扫描数据。 ascends 算法接触的数据更少,效果更好。
升序:
很明显我的算法存在问题,它必须跟踪大量升序值。值和目标之间的距离较短解释了线性搜索的良好性能。 Ascends 仍然无法处理很长的段。
更好的算法
我的算法没有理由必须对数据进行线性搜索。它已排序,我们只需从列表末尾删除较小的值即可。
但我突然想到,我们可以重用之前计算出的 B 值,而不是构建新的数据结构。如果j> i,A[i]> A[j]则B[i]> B[j]。
我的基准测试结果:
上升部分
Cyborg 有一个非常有趣的想法来利用上升部分。我认为他的任何测试用例都没有真正表现出他所追求的行为。我认为这些部分不够长,无法利用。但我认为真实的数据很可能有这样的部分,所以利用它会非常有帮助。
但我认为这不会起作用。准备进行二分查找所需的数据需要 O(n) 时间。如果我们多次进行二分搜索,那就没问题,但是一旦我们在升序部分的中间找到一个值,我们就永远不会重新访问左侧的任何内容。因此,即使使用二分搜索,我们也最多花费 O(n) 时间处理数据。
如果构建必要的数据比稍后扫描升序部分更便宜,那么它就可以工作。但扫描非常便宜,您将很难想出一种更便宜的处理上升部分的方法。
Insufficiently tested, use at own risk.
Lower memory version:
Faster version:
Even faster version:
Credit to cyborg for some of the ideas used here.
Algorithm Difference
cyborg showed significant differences between the various algorithms depending on the data fed into them. I gathered some stats from the running algorithms to see if I could figure out what is going on.
Random data:
Since the lists are really short, the ascents algorithm mostly decays to a linear search. It does clear out ascents which cannot be used in the future, so its still noticably better then a linear search.
Oscillating:
The oscillations tend to put the different pieces further apart. This naturally hampers the linear search algorithm. Both of the "smarter" algorithms have to keep track of additional data. My algorithm decays considerably since it scans over the data each time. The ascends algorithm touch less of its data and does better.
Ascending:
Its clear why my algorithm has issues, it has to track a huge number of ascending values. The short distance between the value and target explains the good performance of a linear search. Ascends is still not working with very long segments.
Better algorithms
There is no reason for my algorithm to have to a linear search over the data. It is sorted and we just need remove to small values from the end of the list.
But it occurs to me that we can reuse previous calculated values of B instead of constructing new data structures. If j > i, A[i] > A[j] then B[i] > B[j].
My benchmark results:
Ascending Sections
Cyborg had the very interesting idea to take advantage of ascending segments. I don't think any of his test cases really exhibit the behaviour he was after there. I don't think the sections were long enough to take advantage of. But I figure real data may well have such sections so taking advantage of it would be really helpful.
But I don't think it'll work. It takes O(n) time to prepare the necessary data to do the binary search. That would be fine if we do the binary search many times, but once we find a value in the middle of the ascending section, we never revisit anything to the left. As a result, even with a binary search we spend at most O(n) time processing the data.
It could work if its less expensive to build the neccesary data then to do scan over the ascending section later. But the scan is pretty cheap and you'll be hard pressed to come up with a way of handling ascending sections that's less expensive.
@Winston Ewert 的
future8
方法是 O(n) (!),并且比我们之前的所有建议都要好。为了证明它的复杂度为 O(n),请观察对于任何B[target]
值,内部while
循环最多执行一次。我的旧答案:
这是三种方法的基准(在@Winston Ewert 和我之间进行乒乓球比赛之后):
在不同条件下,其中每一个都比其他要快得多。如果系列是随机的,那么“完整列表”(future6)是最快的。如果系列振荡,那么“升序列表”(future2)是最快的。如果级数趋于上升,则向量化(future7)是最快的。
如果数据是股票报价,我会选择“向量化”(future7),因为股票有上升趋势,而且它很简单并且在所有条件下都表现合理。
输出:
代码:
@Winston Ewert 's
future8
method is O(n) (!), and is better than all of our previous proposals here. To prove it's O(n), observe that the innerwhile
loop is executed at most once for any value ofB[target]
.My old answer:
Here is a benchmark of three approaches (after a ping pong between @Winston Ewert and me):
Each of these is significantly faster than the others under different conditions. If the series is random, then "full list" (future6) is fastest. If the series oscillates, then "ascending list" (future2) is fastest. If the series tends to ascend, then vectorize (future7) is fastest.
If the data is stock quotes, I would go with "vectorize" (future7), because stocks have an ascending trend, and because it's simple and performs reasonably under all conditions.
Output:
Code: