From: Emanuele O. <oli...@it...> - 2006-07-11 09:32:56
|
Hi, I don't understand how to use argsort results. I have a 2D matrix and I want to sort values in each row and obtain the index array of that sorting. Argsort(1) is what I need, but the problem is how to use its result in order to obtain a sorted matrix. Here is the simple example: A = array([[2,3,1],[5,4,6]]) indexes = a.argsort(1) now indexes is: array([[2, 0, 1], [1, 0, 2]]) I'd like to apply indexes to A and obtain: array([[1, 2, 3], [4, 5, 6]]) or better, I'm interested both in a subset of indexes, i.e. indexes[:,1:], and the related values of A matrix. How can I do this? If I simpy say: A[indexes] I get an IndexError. Thanks in advance, Emanuele P.S. numpy.__version__ is '0.9.8'. |
From: Pau G. <pau...@gm...> - 2006-07-11 09:40:46
|
here goes a first try: A[arange(2)[:,newaxis],indexes] pau On 7/11/06, Emanuele Olivetti <oli...@it...> wrote: > Hi, > I don't understand how to use argsort results. I have a 2D matrix and > I want to sort values in each row and obtain the index array of that > sorting. Argsort(1) is what I need, but the problem is how to use its > result in order to obtain a sorted matrix. Here is the simple example: > > A = array([[2,3,1],[5,4,6]]) > indexes = a.argsort(1) > > now indexes is: > array([[2, 0, 1], > [1, 0, 2]]) > > I'd like to apply indexes to A and obtain: > array([[1, 2, 3], > [4, 5, 6]]) > > or better, I'm interested both in a subset of indexes, i.e. indexes[:,1:], and > the related values of A matrix. > > How can I do this? If I simpy say: A[indexes] I get an IndexError. > > Thanks in advance, > > Emanuele > > P.S. numpy.__version__ is '0.9.8'. > > > ------------------------------------------------------------------------- > Using Tomcat but need to do more? Need to support web services, security? > Get stuff done quickly with pre-integrated technology to make your job easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion > |
From: Emanuele O. <oli...@it...> - 2006-07-11 09:57:33
|
Wow. I have to study much more indexing. It works pretty well. Just to help indexing newbie like on using your advice: A[arange(A.shape[0])[:,newaxis],indexes] Thanks a lot! Emanuele Pau Gargallo wrote: > here goes a first try: > > A[arange(2)[:,newaxis],indexes] |
From: Pau G. <pau...@gm...> - 2006-07-13 08:37:06
|
On 7/12/06, Victoria G. Laidler <la...@st...> wrote: > Hi, > > Pardon me if I'm reprising an earlier discussion, as I'm new to the list. > > But is there a reason that this obscure syntax > > A[arange(2)[:,newaxis],indexes] > > A[arange(A.shape[0])[:,newaxis],indexes] > > is preferable to the intuitively reasonable thing that the Original > Poster did? > > A[indexes] > i don't think so. The obscure syntax is just a way you can solve the problem with the current state of NumPy. Of course, a more clearer syntax would be better, but for this, something in NumPy should be changed. This other syntax is longer but clearer: ind = indices(A.shape) ind[ax] = A.argsort(axis=ax) A[ind] Which brings me to the question: Would it be reasonable if argsort returned the complete tuple of indices, so that A[A.argsort(ax)] would work ? pau |
From: Stefan v. d. W. <st...@su...> - 2006-07-11 10:20:23
|
On Tue, Jul 11, 2006 at 11:32:48AM +0200, Emanuele Olivetti wrote: > Hi, > I don't understand how to use argsort results. I have a 2D matrix and > I want to sort values in each row and obtain the index array of that > sorting. Argsort(1) is what I need, but the problem is how to use its > result in order to obtain a sorted matrix. Here is the simple example: >=20 > A =3D array([[2,3,1],[5,4,6]]) > indexes =3D a.argsort(1) >=20 > now indexes is: > array([[2, 0, 1], > [1, 0, 2]]) >=20 > I'd like to apply indexes to A and obtain: > array([[1, 2, 3], > [4, 5, 6]]) >=20 > or better, I'm interested both in a subset of indexes, i.e. indexes[:,1= :], and > the related values of A matrix. >=20 > How can I do this? If I simpy say: A[indexes] I get an IndexError. Something's not quite right here. The argsort docstring states that: argsort(a,axis=3D-1) return the indices into a of the sorted array along the given axis, so that take(a,result,axis) is the sorted array= . But N.take(A,A.argsort()) breaks. Either this is a bug, or the docstring needs to be updated. Cheers St=E9fan |
From: Pau G. <pau...@gm...> - 2006-07-11 10:37:25
|
On 7/11/06, Stefan van der Walt <st...@su...> wrote: > On Tue, Jul 11, 2006 at 11:32:48AM +0200, Emanuele Olivetti wrote: > > Hi, > > I don't understand how to use argsort results. I have a 2D matrix and > > I want to sort values in each row and obtain the index array of that > > sorting. Argsort(1) is what I need, but the problem is how to use its > > result in order to obtain a sorted matrix. Here is the simple example: > > > > A =3D array([[2,3,1],[5,4,6]]) > > indexes =3D a.argsort(1) > > > > now indexes is: > > array([[2, 0, 1], > > [1, 0, 2]]) > > > > I'd like to apply indexes to A and obtain: > > array([[1, 2, 3], > > [4, 5, 6]]) > > > > or better, I'm interested both in a subset of indexes, i.e. indexes[:,1= :], and > > the related values of A matrix. > > > > How can I do this? If I simpy say: A[indexes] I get an IndexError. > > Something's not quite right here. The argsort docstring states that: > > argsort(a,axis=3D-1) return the indices into a of the sorted array > along the given axis, so that take(a,result,axis) is the sorted array= . > > But > > N.take(A,A.argsort()) breaks. Either this is a bug, or the docstring > needs to be updated. > > Cheers > St=E9fan > I think the docstring is wrong, because take doesn't do that. if you N.take(A,A.argsort(1), 1), it doesn't break but it doesn't sort A neither. Take seems to peek entire columns, but take's docstring is missing. For the argsort docstring, it may be usefull to indicate that if one do >>> ind =3D indices(A.shape) >>> ind[ax] =3D A.argsort(axis=3Dax) then A[ind] is the sorted array. pau |
From: Stefan v. d. W. <st...@su...> - 2006-07-11 11:11:12
|
On Tue, Jul 11, 2006 at 12:37:23PM +0200, Pau Gargallo wrote: > > Something's not quite right here. The argsort docstring states that: > > > > argsort(a,axis=3D-1) return the indices into a of the sorted arra= y > > along the given axis, so that take(a,result,axis) is the sorted a= rray. > > > > But > > > > N.take(A,A.argsort()) breaks. Either this is a bug, or the docstring > > needs to be updated. > > > > Cheers > > St=E9fan > > >=20 > I think the docstring is wrong, because take doesn't do that. > if you N.take(A,A.argsort(1), 1), it doesn't break but it doesn't sort > A neither. >=20 > Take seems to peek entire columns, but take's docstring is missing. >=20 > For the argsort docstring, it may be usefull to indicate that if one do > >>> ind =3D indices(A.shape) > >>> ind[ax] =3D A.argsort(axis=3Dax) > then A[ind] is the sorted array. We can always adapt the documentation at http://numeric.scipy.org/numpydoc/numpy-9.html#pgfId-36425 into a docstring. I'll file a ticket. Cheers St=E9fan |
From: Victoria G. L. <la...@st...> - 2006-07-12 21:42:19
|
Hi, Pardon me if I'm reprising an earlier discussion, as I'm new to the list. But is there a reason that this obscure syntax A[arange(2)[:,newaxis],indexes] A[arange(A.shape[0])[:,newaxis],indexes] is preferable to the intuitively reasonable thing that the Original Poster did? A[indexes] Doesn't it violate the pythonic "principle of least surprise" for the simpler syntax not to work? As a casual numpy user, I can't imagine being able to remember the obscure syntax in order to use the result of an argsort. curiously, Vicki Laidler (numarray user who came from IDL) Emanuele Olivetti wrote: >Wow. I have to study much more indexing. It works pretty well. > >Just to help indexing newbie like on using your advice: >A[arange(A.shape[0])[:,newaxis],indexes] > >Thanks a lot! > >Emanuele > >Pau Gargallo wrote: > > >>here goes a first try: >> >>A[arange(2)[:,newaxis],indexes] >> >> > > >------------------------------------------------------------------------- >Using Tomcat but need to do more? Need to support web services, security? >Get stuff done quickly with pre-integrated technology to make your job easier >Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo >http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 >_______________________________________________ >Numpy-discussion mailing list >Num...@li... >https://lists.sourceforge.net/lists/listinfo/numpy-discussion > > |
From: Travis O. <oli...@ie...> - 2006-07-13 09:07:30
|
Pau Gargallo wrote: > On 7/12/06, Victoria G. Laidler <la...@st...> wrote: > >> Hi, >> >> Pardon me if I'm reprising an earlier discussion, as I'm new to the list. >> >> But is there a reason that this obscure syntax >> >> A[arange(2)[:,newaxis],indexes] >> >> A[arange(A.shape[0])[:,newaxis],indexes] >> >> is preferable to the intuitively reasonable thing that the Original >> Poster did? >> >> A[indexes] >> >> > > i don't think so. > The obscure syntax is just a way you can solve the problem with the > current state of NumPy. Of course, a more clearer syntax would be > better, but for this, something in NumPy should be changed. > > This other syntax is longer but clearer: > ind = indices(A.shape) > ind[ax] = A.argsort(axis=ax) > A[ind] > > Which brings me to the question: > > Would it be reasonable if argsort returned the complete tuple of > indices, so that > A[A.argsort(ax)] would work ? > > I think this is reasonable. We would need a way for the argsort() function to work as it does now. I'm not sure if anybody actually uses the multidimensional behavior of argsort now, but it's been in Numeric for a long time. -Travis |
From: Pau G. <pau...@gm...> - 2006-07-13 12:09:10
|
On 7/13/06, Travis Oliphant <oli...@ie...> wrote: > Pau Gargallo wrote: > > On 7/12/06, Victoria G. Laidler <la...@st...> wrote: > > > >> Hi, > >> > >> Pardon me if I'm reprising an earlier discussion, as I'm new to the list. > >> > >> But is there a reason that this obscure syntax > >> > >> A[arange(2)[:,newaxis],indexes] > >> > >> A[arange(A.shape[0])[:,newaxis],indexes] > >> > >> is preferable to the intuitively reasonable thing that the Original > >> Poster did? > >> > >> A[indexes] > >> > >> > > > > i don't think so. > > The obscure syntax is just a way you can solve the problem with the > > current state of NumPy. Of course, a more clearer syntax would be > > better, but for this, something in NumPy should be changed. > > > > This other syntax is longer but clearer: > > ind = indices(A.shape) > > ind[ax] = A.argsort(axis=ax) > > A[ind] > > > > Which brings me to the question: > > > > Would it be reasonable if argsort returned the complete tuple of > > indices, so that > > A[A.argsort(ax)] would work ? > > > > > I think this is reasonable. We would need a way for the argsort() > function to work as it does now. I'm not sure if anybody actually uses > the multidimensional behavior of argsort now, but it's been in Numeric > for a long time. > actually I use the multidimensional behavior of argmin and argmax in its current state, and found it useful as it is, even if A[A.argmax(ax)] doesn't work. May be a keyword could be added, so that A.argxxx( axis=ax, indices=True ) returns the tuple of indices. (The keyword name and default should be discussed) I don't know if that's *the* way, but ... pau |
From: Eric F. <ef...@ha...> - 2006-07-13 17:51:40
|
> > Would it be reasonable if argsort returned the complete tuple of > indices, so that > A[A.argsort(ax)] would work ? +1 This is the behavior one would naturally expect. Eric |