Home > Back-end >  Find index within full array of the argmax of an array subset
Find index within full array of the argmax of an array subset

Time:11-21

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

CodePudding user response:

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()]
  • Related