0

Im trying to write a code with numpy where it outputs the maximum value between indexes. I think using argmax could be usable. However I do not know how I can use slices without using a for loop in python. If there is a pandas function for this it could be useable too. I want to make the computation as fast as possible.

list_ = np.array([9887.89, 9902.99, 9902.99, 9910.23, 9920.79, 9911.34, 9920.01, 9927.51, 9932.3, 9932.33, 9928.87, 9929.22, 9929.22, 9935.24, 9935.24, 9935.26, 9935.26, 9935.68, 9935.68, 9940.5])
indexes = np.array([0, 5, 10, 19])

Expected result:

Max number between index(0 - 5):  9920.79 at index 5
Max number between index(5 - 10): 9932.33 at index 10
Max number between index(10 - 19): 9940.5 at index 19

2 Answers 2

1

You can use reduceat directly yo your array without the need to splice/split it:

np.maximum.reduceat(list_,indexes[:-1])

output:

array([9932.33, 9929.22, 9940.5 ])
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that the first (zero) index and the last index is specified in the indexes array,

import numpy as np

list_ = np.array([9887.89, 9902.99, 9902.99, 9910.23, 9920.79, 9911.34, 9920.01, 9927.51, 9932.3, 9932.33, 9928.87, 9929.22, 9929.22, 9935.24, 9935.24, 9935.26, 9935.26, 9935.68, 9935.68, 9940.5])
indexes = np.array([0, 5, 10, 19])

chunks = np.split(list_, indexes[1:-1])
print([c.max() for c in chunks])

max_ind = [c.argmax() for c in chunks]
print(max_ind + indexes[:-1])

It's not necessary that each chunk will have the same size with an arbitrary specification of indices. So The vectorization benefits of numpy is going to be lost in there one way or another (Since you can't have a numpy array where each element is of a different size in memory which also has all the benefits of vectorization).

At least one for loop is going to be necessary, I think. However, you can use split, to make the splitting a numpy-optimized operation.

4 Comments

Thank you it works is there a way I could get the indexes of the max values as well?
Indices in the original array right? Not the sub-chunks?
Modified the answer to also print out the indexes.
Thank you I will try to find a way if I could get rid of the list comprehension other than that the code is very well made thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.