
On Mon, Jan 30, 2012 at 11:31 AM, Ted To <rainexpected@theo.to> wrote:
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
You'd want to update your mask appropriately to get everything you want to select, one criteria at a time e.g.: mask = a[:,0] == 1 mask &= a[:,1] == 1960 Alternatively: mask = (a[:,0] == 1) & (a[:,1] == 1960) but be careful with the parens, & and | are normally high-priority bitwise operators and if you leave the parens out, it will try to bitwise-and 1 and a[:,1] and throw an error. If you've got a ton of parameters, you can combine these more aesthetically with: mask = (a[:,[0,1]] == [1, 1960]).all(axis=1) ~Brett