[Numpy-discussion] Argsort

Robert Kern robert.kern@gmail....
Thu Feb 5 17:15:56 CST 2009


On Thu, Feb 5, 2009 at 17:06, Ryan May <rmay31@gmail.com> wrote:
> Hi,
>
> Ok, what am I missing here:
>
> x = np.array([[4,2],[5,3]])
> x[x.argsort(1)]
>
> array([[[5, 3],
>        [4, 2]],
>
>       [[5, 3],
>        [4, 2]]])
>
> I was expecting:
>
> array([[2,4],[3,5]])
>
> Certainly not a 3D array.  What am I doing wrong?

Remember that x[i] applies the index array i to the first axis of x.
In order to apply i to the second axis, you need an argument in the
first position, too, to give the indices that apply to the first axis.
Remember that the arguments will be broadcast against each other.

In [11]: x[ [[0],[1]], x.argsort(1)]
Out[11]:
array([[2, 4],
       [3, 5]])

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