conditional array indexing

Very new user to Numpy, so excuse the question. It relates to a fundamental issue which I have having trouble getting a grip on. Specifically, say I have the following: a = ([1,2,1,2,4,3,1,2,3,2,3,2]) b = ([4,3,5,2,4,5,3,6,3,2,5,6]) c = random variable of length=len(b) I would like to get the value of C associated with the lowest value of b within groupings of a. That is, there are three incidents of "1" in the a array. The lowest value of b associated with the value 1 in the a index is "3". This occurs in index value 6 on the a and b arrays. Now, I would like to then know the value of c[6]. Obviously the real problem is far more complex with len(a)==5000. Any ideas? Thanks in advance. Michaell

import Numeric, sys a = Numeric.array([1,2,1,2,4,3,1,2,3,2,3,2]) b = Numeric.array([4,3,5,2,4,5,3,6,3,2,5,6]) c = Numeric.arange(len(b)) print c[Numeric.argmin(Numeric.where(a==1, b, sys.maxint))] -----Original Message----- From: numpy-discussion-admin@lists.sourceforge.net [mailto:numpy-discussion-admin@lists.sourceforge.net] On Behalf Of Michaell Taylor Sent: Monday, January 28, 2002 12:59 PM To: numpy-discussion@lists.sourceforge.net Subject: [Numpy-discussion] conditional array indexing Very new user to Numpy, so excuse the question. It relates to a fundamental issue which I have having trouble getting a grip on. Specifically, say I have the following: a = ([1,2,1,2,4,3,1,2,3,2,3,2]) b = ([4,3,5,2,4,5,3,6,3,2,5,6]) c = random variable of length=len(b) I would like to get the value of C associated with the lowest value of b within groupings of a. That is, there are three incidents of "1" in the a array. The lowest value of b associated with the value 1 in the a index is "3". This occurs in index value 6 on the a and b arrays. Now, I would like to then know the value of c[6]. Obviously the real problem is far more complex with len(a)==5000. Any ideas? Thanks in advance. Michaell _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion

Thanks for your quick reply. This looks vaguely S-Plus-like, with which I am familar. I wasn't too clear in my question however, I would actually like a vector returned which would contain the values of c for the minimun value of b, within each category of a. Something like: for i in range(1:5000): # range of values of a d = c[Numeric.argmin(Numeric.where(a==i, b, sys.maxint))] Seems like I could hack a fix using such a vector, but I would guess that speed would be an issue. Essentially the functionality implied by GROUP BY in sql egen in stata lapply() in S-Plus. Paul F. Dubois wrote:
-- ========================================= Michaell Taylor, PhD Senior Economist, Reis, New York, USA

The O'Reilly Open Source Convention (July 22-26, 2002 -- San Diego, CA) is accepting proposals for tutorials, talks, panels, and lightning talks. See the Call for Participation in the Python and Zope track on python.org. Proposals are due by March 1, so don't wait a moment longer! CFP URL: http://www.python.org/workshops/oscon2002/cfp.html --david ascher

import Numeric, sys a = Numeric.array([1,2,1,2,4,3,1,2,3,2,3,2]) b = Numeric.array([4,3,5,2,4,5,3,6,3,2,5,6]) c = Numeric.arange(len(b)) print c[Numeric.argmin(Numeric.where(a==1, b, sys.maxint))] -----Original Message----- From: numpy-discussion-admin@lists.sourceforge.net [mailto:numpy-discussion-admin@lists.sourceforge.net] On Behalf Of Michaell Taylor Sent: Monday, January 28, 2002 12:59 PM To: numpy-discussion@lists.sourceforge.net Subject: [Numpy-discussion] conditional array indexing Very new user to Numpy, so excuse the question. It relates to a fundamental issue which I have having trouble getting a grip on. Specifically, say I have the following: a = ([1,2,1,2,4,3,1,2,3,2,3,2]) b = ([4,3,5,2,4,5,3,6,3,2,5,6]) c = random variable of length=len(b) I would like to get the value of C associated with the lowest value of b within groupings of a. That is, there are three incidents of "1" in the a array. The lowest value of b associated with the value 1 in the a index is "3". This occurs in index value 6 on the a and b arrays. Now, I would like to then know the value of c[6]. Obviously the real problem is far more complex with len(a)==5000. Any ideas? Thanks in advance. Michaell _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion

Thanks for your quick reply. This looks vaguely S-Plus-like, with which I am familar. I wasn't too clear in my question however, I would actually like a vector returned which would contain the values of c for the minimun value of b, within each category of a. Something like: for i in range(1:5000): # range of values of a d = c[Numeric.argmin(Numeric.where(a==i, b, sys.maxint))] Seems like I could hack a fix using such a vector, but I would guess that speed would be an issue. Essentially the functionality implied by GROUP BY in sql egen in stata lapply() in S-Plus. Paul F. Dubois wrote:
-- ========================================= Michaell Taylor, PhD Senior Economist, Reis, New York, USA

The O'Reilly Open Source Convention (July 22-26, 2002 -- San Diego, CA) is accepting proposals for tutorials, talks, panels, and lightning talks. See the Call for Participation in the Python and Zope track on python.org. Proposals are due by March 1, so don't wait a moment longer! CFP URL: http://www.python.org/workshops/oscon2002/cfp.html --david ascher
participants (3)
-
David Ascher
-
Michaell Taylor
-
Paul F. Dubois