[Numpy-discussion] Array min from argmin along an axis?

Robert Kern robert.kern at gmail.com
Tue Dec 13 17:27:29 EST 2011


On Tue, Dec 13, 2011 at 22:11, Ken Basye <kbasye1 at jhu.edu> wrote:
> Hi folks,
>     I need an efficient way to get both the min and argmin of a 2-d
> array along one axis.  It seemed to me that the way to do this was to
> get the argmin and then use it to index into the array to get the min,
> but I can't figure out how to do it.  Here's my toy example:

[~]
|1> x = np.arange(25).reshape((5,5))

[~]
|2> y = np.abs(x - x.T)

[~]
|3> y
array([[ 0,  4,  8, 12, 16],
       [ 4,  0,  4,  8, 12],
       [ 8,  4,  0,  4,  8],
       [12,  8,  4,  0,  4],
       [16, 12,  8,  4,  0]])

[~]
|4> i = np.argmin(y, axis=0)

[~]
|5> y[i, np.arange(y.shape[1])]
array([0, 0, 0, 0, 0])

[~]
|6> y[np.argmin(y, axis=0), np.arange(y.shape[1])]
array([0, 0, 0, 0, 0])

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list