在移动的时间间隔使用python查找最大(和最小) [英] Find max(and min) on the moving interval using python
问题描述
我有一个像
[5.5, 6.0, 6.0, 6.5, 6.0, 5.5, 5.5, 5.0, 4.5].
此阵列的所有数字0.5不同,并且两个连续数的最大差也是0.5(它们可以是相同的;如在实施例)。并有一个移动的时间间隔,或盒子,它涵盖,例如,3个连续的数字,是这样的:
all numbers of this array differ by 0.5, and the maximum difference of two successive numbers is also 0.5(they can be same; as in the example). and there is a moving interval, or box, which covers, for example, 3 successive numbers, like this:
[(5.5, 6.0, 6.0), 6.5, 6.0, 5.5, 5.5, 5.0, 4.5] # min: 5.5, max: 6.0
和框移动向右一个接一个:
and the box moves toward right one by one:
[5.5, (6.0, 6.0, 6.5), 6.0, 5.5, 5.5, 5.0, 4.5] # min: 6.0, max: 6.5
[5.5, 6.0, (6.0, 6.5, 6.0), 5.5, 5.5, 5.0, 4.5] # min: 6.0, max: 6.5
问题是,我怎么能找到的数字箱内的最小值和最大值,每次框移动?
the question is, how can I find the min and max of the numbers inside the box for each time box moves?
我可以处理它时,框和数组的大小是小像这样的例子,但我需要将此喜欢数组大小100000箱尺寸10000使用我的方法(我每次计算最大值和最小值使用循环每箱经过时间),花了太多时间(我有像100多个阵列做,需要反复运行)。有一些时间限制,所以我需要像一次计算在0.5秒运行它。
I can handle it when the size of box and array is small like this example, but I need to apply this to like array size 100000 and box size 10000. using my method(I calculate every max and min using for-loop for each time box passes), it took too much time(I have like 100 more array to do and need to run repeatedly). There is some time limit, so I need to run it like one calculation in 0.5 sec.
推荐答案
有一个看的从熊猫的滚动窗口:
Have a look at the rolling windows from pandas:
>>> import pandas as pd
>>> L = [5.5, 6.0, 6.0, 6.5, 6.0, 5.5, 5.5, 5.0, 4.5]
>>> a = pd.DataFrame(L)
>>> pd.rolling_max(a, 3)
0
0 NaN
1 NaN
2 6.0
3 6.5
4 6.5
5 6.5
6 6.0
7 5.5
8 5.5
>>> pd.rolling_min(a, 3)
0
0 NaN
1 NaN
2 5.5
3 6.0
4 6.0
5 5.5
6 5.5
7 5.0
8 4.5
这篇关于在移动的时间间隔使用python查找最大(和最小)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!