[Numpy-discussion] conditional array indexing
michaell.taylor at reis.com
Tue Jan 29 08:46:02 EST 2002
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:
> 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 at lists.sourceforge.net
> [mailto:numpy-discussion-admin at lists.sourceforge.net] On Behalf Of
> Michaell Taylor
> Sent: Monday, January 28, 2002 12:59 PM
> To: numpy-discussion at lists.sourceforge.net
> Subject: [Numpy-discussion] conditional array indexing
> Very new user to Numpy, so excuse the question. It relates to a
> 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
> array. The lowest value of b associated with the value 1 in the a index
> "3". This occurs in index value 6 on the a and b arrays. Now, I would
> to then know the value of c.
> Obviously the real problem is far more complex with len(a)==5000.
> Any ideas?
> Thanks in advance.
> Numpy-discussion mailing list Numpy-discussion at lists.sourceforge.net
Michaell Taylor, PhD
Senior Economist, Reis, New York, USA
More information about the NumPy-Discussion