How to find last occurrence of maximum value in a numpy.ndarray

numpy.argmax only returns the index of the first occurrence. You could apply argmax to a reversed view of the array:

import numpy as np
a = np.array([0,0,4,4,4,4,2,2,2,2])
b = a[::-1]
i = len(b) - np.argmax(b) - 1
i     # 5
a[i:] # array([4, 2, 2, 2, 2])

Note numpy doesn’t copy the array but instead creates a view of the original with a stride that accesses it in reverse order.

id(a) == id(b.base) # True

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)