argmax_last¶
-
mdtools.numpy_helper_functions.
argmax_last
(a, axis=None, out=None)[source]¶ Get the indices of the maximum values along an axis.
Contrarily to
numpy.argmax()
, this function returns the indices of the last occurrence of the maximum value.- Parameters
a (array_like) – Input array.
axis (int, optional) – By default, the index is into the flattened array, otherwise along the specified axis.
out (numpy.ndarray, optional) – If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.
- Returns
index_array – Array of indices into the array. It has the same shape as
a.shape
with the dimension along axis removed.- Return type
numpy.ndarray of ints
See also
numpy.argmax()
Returns indices of the first occurence of the maximum value
argmin_last()
Same as
argmax_last()
but for minimum valuesnumpy.unravel_index()
Convert the returned index ix to a tuple of index arrays suitable to index a multidimensional input array a if axis was
None
ix_along_axis_to_global_ix()
Same as
numpy.unravel_index()
, but to be used when axis was notNone
.
Notes
In case of multiple occurrences of the maximum values, the indices corresponding to the last occurrence are returned. This is the opposite of what
numpy.argmax()
does.Examples
>>> a = np.arange(4) >>> a[1] = 3 >>> a array([0, 3, 2, 3]) >>> mdt.nph.argmax_last(a) 3 >>> np.argmax(a) 1
2-dimensional case:
>>> a = np.eye(3, 4, 0, dtype=int) + np.eye(3, 4, 2, dtype=int) >>> a array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]) >>> mdt.nph.argmax_last(a) 10 >>> mdt.nph.argmax_last(a, axis=0) array([0, 1, 2, 1]) >>> mdt.nph.argmax_last(a, axis=1) array([2, 3, 2])
3-dimensional case:
>>> a = np.array([[[1, 1, 0], ... [0, 1, 1]], ... ... [[1, 0, 1], ... [1, 1, 0]]]) >>> mdt.nph.argmax_last(a) 10 >>> mdt.nph.argmax_last(a, axis=0) array([[1, 0, 1], [1, 1, 0]]) >>> mdt.nph.argmax_last(a, axis=1) array([[0, 1, 1], [1, 1, 0]]) >>> mdt.nph.argmax_last(a, axis=2) array([[1, 2], [2, 1]])