argmin_last

mdtools.numpy_helper_functions.argmin_last(a, axis=None, out=None)[source]

Get the indices of the minimum values along an axis.

Contrarily to numpy.argmin(), this function returns the indices of the last occurrence of the minimum 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 (numpy.ndarray of ints) – Array of indices into the array. It has the same shape as a.shape with the dimension along axis removed.

See also

numpy.argmin()

Returns indices of the first occurrence of the minium value

argmax_last()

Same as argmin_last() but for maximum values

numpy.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

mdtools.numpy_helper_functions.ix_along_axis_to_global_ix()

Same as numpy.unravel_index(), but to be used when axis was not None.

Notes

In case of multiple occurrences of the minimum values, the indices corresponding to the last occurrence are returned. This is the opposite of what numpy.argmin() does.

Examples

>>> a = -np.arange(4)
>>> a[1] = -3
>>> a
array([ 0, -3, -2, -3])
>>> mdt.nph.argmin_last(a)
3
>>> np.argmin(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.argmin_last(a)
10
>>> mdt.nph.argmin_last(a, axis=0)
array([0, 1, 2, 1])
>>> mdt.nph.argmin_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.argmin_last(a)
10
>>> mdt.nph.argmin_last(a, axis=0)
array([[1, 0, 1],
       [1, 1, 0]])
>>> mdt.nph.argmin_last(a, axis=1)
array([[0, 1, 1],
       [1, 1, 0]])
>>> mdt.nph.argmin_last(a, axis=2)
array([[1, 2],
       [2, 1]])