[Numpy-discussion] Addressing arrays
Ted To
rainexpected@theo...
Mon Jan 30 11:31:55 CST 2012
On 01/30/2012 12:13 PM, Brett Olsen wrote:
> On Mon, Jan 30, 2012 at 10:57 AM, Ted To <rainexpected@theo.to> wrote:
>> Sure thing. To keep it simple suppose I have just a two dimensional
>> array (time,output):
>> [(1,2),(2,3),(3,4)]
>> I would like to look at all values of output for which, for example time==2.
>>
>> My actual application has a six dimensional array and I'd like to look
>> at the contents using one or more of the first three dimensions.
>>
>> Many thanks,
>> Ted
>
> Couldn't you just do something like this with boolean indexing:
>
> In [1]: import numpy as np
>
> In [2]: a = np.array([(1,2),(2,3),(3,4)])
>
> In [3]: a
> Out[3]:
> array([[1, 2],
> [2, 3],
> [3, 4]])
>
> In [4]: mask = a[:,0] == 2
>
> In [5]: mask
> Out[5]: array([False, True, False], dtype=bool)
>
> In [6]: a[mask,1]
> Out[6]: array([3])
>
> ~Brett
Thanks! That works great if I only want to search over one index but I
can't quite figure out what to do with more than a single index. So
suppose I have a labeled, multidimensional array with labels 'month',
'year' and 'quantity'. a[['month','year']] gives me an array of indices
but "a[['month','year']]==(1,1960)" produces "False". I'm sure I simply
don't know the proper syntax and I apologize for that -- I'm kind of new
to numpy.
Ted
More information about the NumPy-Discussion
mailing list