I have a array like
[5.5, 6.0, 6.0, 6.5, 6.0, 5.5, 5.5, 5.0, 4.5].
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?
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.
Use Python's min() and max() to find smallest and largest values in your data. Call min() and max() with a single iterable or with any number of regular arguments.
numpy. amax() will find the max value in an array, and numpy. amin() does the same for the min value.
Python max() FunctionThe max() function returns the item with the highest value, or the item with the highest value in an iterable. If the values are strings, an alphabetically comparison is done.
nanmax() to find the maximum values while ignoring nan values, as well as np. argmax() or . argmax() to find the indices of the maximum values. You won't be surprised to learn that NumPy has an equivalent set of minimum functions: np.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With