Find index within full array of the argmax of an array subset

Question:

Goal: to find the index of the highest value in 1d array from index 25 to [-1]

The way I do it is wrong, I get the index of the highest value of the slice.

If there is no other way than slicing it, how do I then get the correct index of the original array?

ipython code example

Asked By: traderblakeq

||

Answers:

If you know you’re indexing the array from index 25 on, you could add 25 to the result:

argmax = close[25:].argmax() + 25

But if you’re using an arbitrary slice of your array, another way to do this would be to create a similar array of indices, then slice that the same way:

indices = np.arange(len(close))
# some complicated slicing
sliced = close[2:-7:4]
# apply the same slicing:
sliced_indices = indices[2:-7:4]

# now, get the index in the original array of the argmax from the subset
argmax = sliced_indices[sliced.argmax()]
Answered By: Michael Delgado
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.