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

Torgil Svensson torgil.svensson at gmail.com
Fri Dec 16 21:08:17 EST 2011


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

Can xrange in this case save me from creating a temporary array here
or doesn't it matter?

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

//Torgil

On Tue, Dec 13, 2011 at 11:27 PM, Robert Kern <robert.kern at gmail.com> wrote:
> 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
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list