However it only evaluates along a single axis and returns the index of the minimum value along a single row/column whereas I wish to evaluate the whole array and return the lowest value not the indices.
numpy.argmin does not by default evaluate along a single axis, the default is to evaluate along the flattened matrix and it returns the linear index in the flattened array; from the numpy docs that you linked:
By default, the index is into the flattened array, otherwise along the specified axis.
Either way, use numpy.amin or numpy.min to return the minimum value, or equivalently for an array arrname use arrname.min(). As you mentioned, numpy.argmin returns the index of the minimum value (of course, you can then use this index to return the minimum value by indexing your array with it). You could also flatten into a single dimension array with arrname.flatten() and pass that into the built-in min function.
The four following methods produce what you want.
import numpy as np
values = np.array([
[8,2,3,4,5,6],
[3,6,6,7,2,6],
[3,8,5,1,2,9],
[6,4,2,7,8,3]])
values.min() # = 1
np.min(values) # = 1
np.amin(values) # = 1
min(values.flatten()) # = 1
Answer from alkasm on Stack OverflowHowever it only evaluates along a single axis and returns the index of the minimum value along a single row/column whereas I wish to evaluate the whole array and return the lowest value not the indices.
numpy.argmin does not by default evaluate along a single axis, the default is to evaluate along the flattened matrix and it returns the linear index in the flattened array; from the numpy docs that you linked:
By default, the index is into the flattened array, otherwise along the specified axis.
Either way, use numpy.amin or numpy.min to return the minimum value, or equivalently for an array arrname use arrname.min(). As you mentioned, numpy.argmin returns the index of the minimum value (of course, you can then use this index to return the minimum value by indexing your array with it). You could also flatten into a single dimension array with arrname.flatten() and pass that into the built-in min function.
The four following methods produce what you want.
import numpy as np
values = np.array([
[8,2,3,4,5,6],
[3,6,6,7,2,6],
[3,8,5,1,2,9],
[6,4,2,7,8,3]])
values.min() # = 1
np.min(values) # = 1
np.amin(values) # = 1
min(values.flatten()) # = 1
Alternatively for a non-numpy solution:
>>> a = [[8,2,3,4,5,6],
... [3,6,6,7,2,6],
... [3,8,5,1,2,9],
... [6,4,2,7,8,3]]
>>> mymin = min([min(r) for r in a])
>>> mymin
1
You can use np.unravel_index
print(np.unravel_index(x.argmin(), x.shape))
(2, 1)
You may use np.where:
In [9]: np.where(x == np.min(x))
Out[9]: (array([2]), array([1]))
Also as @senderle mentioned in comment, to get values in an array, you can use np.argwhere:
In [21]: np.argwhere(x == np.min(x))
Out[21]: array([[2, 1]])
Updated:
As OP's times show, and much clearer that argmin is desired (no duplicated mins etc.), one way I think may slightly improve OP's original approach is to use divmod:
divmod(x.argmin(), x.shape[1])
Timed them and you will find that extra bits of speed, not much but still an improvement.
%timeit find_min_idx(x)
1000000 loops, best of 3: 1.1 µs per loop
%timeit divmod(x.argmin(), x.shape[1])
1000000 loops, best of 3: 1.04 µs per loop
If you are really concerned about performance, you may take a look at cython.
alko's answer didn't work for me, so here's what I did:
import numpy as np
array = np.arange(15).reshape(5,3)
x,y = np.unravel_index(np.argmin(array),array.shape)
Seems like you need consecutive min along axis. For your first example:
>>> np.min(np.min(data, axis=1), axis=0)
array([ 0, 1])
For the second:
>>> np.min(np.min(data, axis=1), axis=0)
array([0, 3])
The same expression can be stated like this (with numpy >= 1.7, as pointed out by @Jaime)
>>> np.min(data, axis=(1, 0))
array([0, 3])