[Numpy-discussion] Addressing arrays

2012-01-30 Thread Ted To
Hi,

Is there some straightforward way to access an array by values across a
subset of its dimensions?  For example, if I have a three dimensional
array a=(x,y,z), can I look at the values of z given particular values
for x and y?

Thanks,
Ted
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Addressing arrays

2012-01-30 Thread Chao YUE
I am afraid you have to write index inquire function by yourself. I did
like this.

chao

2012/1/30 Ted To rainexpec...@theo.to

 Hi,

 Is there some straightforward way to access an array by values across a
 subset of its dimensions?  For example, if I have a three dimensional
 array a=(x,y,z), can I look at the values of z given particular values
 for x and y?

 Thanks,
 Ted
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion




-- 
***
Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Addressing arrays

2012-01-30 Thread Malcolm Reynolds
On Mon, Jan 30, 2012 at 3:25 PM, Ted To rainexpec...@theo.to wrote:
 Is there some straightforward way to access an array by values across a
 subset of its dimensions?  For example, if I have a three dimensional
 array a=(x,y,z), can I look at the values of z given particular values
 for x and y?

a[x, y, :] should get you what you want I believe..

Malcolm
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Addressing arrays

2012-01-30 Thread Zachary Pincus
a[x,y,:]

Read the slicing part of the tutorial:
http://www.scipy.org/Tentative_NumPy_Tutorial 
(section 1.6)

And the documentation:
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html



On Jan 30, 2012, at 10:25 AM, Ted To wrote:

 Hi,
 
 Is there some straightforward way to access an array by values across a
 subset of its dimensions?  For example, if I have a three dimensional
 array a=(x,y,z), can I look at the values of z given particular values
 for x and y?
 
 Thanks,
 Ted
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Addressing arrays

2012-01-30 Thread Chao YUE
he is not asking for slicing. he is asking for how to index array by
element value but not element index.

2012/1/30 Zachary Pincus zachary.pin...@yale.edu

 a[x,y,:]

 Read the slicing part of the tutorial:
 http://www.scipy.org/Tentative_NumPy_Tutorial
 (section 1.6)

 And the documentation:
 http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html



 On Jan 30, 2012, at 10:25 AM, Ted To wrote:

  Hi,
 
  Is there some straightforward way to access an array by values across a
  subset of its dimensions?  For example, if I have a three dimensional
  array a=(x,y,z), can I look at the values of z given particular values
  for x and y?
 
  Thanks,
  Ted
  ___
  NumPy-Discussion mailing list
  NumPy-Discussion@scipy.org
  http://mail.scipy.org/mailman/listinfo/numpy-discussion

 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion




-- 
***
Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Addressing arrays

2012-01-30 Thread Zachary Pincus
Ted, can you clarify what you're asking for? Maybe give a trivial example of an 
array and the desired output?

I'm pretty sure this is a slicing question though:
 If I have a three dimensional array a=(x,y,z), can I look at the values of z 
 given particular values for x and y?
Given that element values are scalars in this case, and indices are (x,y,z) 
triples, it seems likely that looking for values of z given an (x,y) pair is 
an slicing-by-index question, no?

For indexing-by-value, fancy indexing with boolean masks is usually the way 
to go... again, Ted (or Chao), if you can describe your indexing needs in a bit 
more detail, it's often easy to find a compact slicing and/or fancy-indexing 
strategy that works well and reasonably efficiently.

Zach



On Jan 30, 2012, at 10:33 AM, Chao YUE wrote:

 he is not asking for slicing. he is asking for how to index array by element 
 value but not element index.
 
 2012/1/30 Zachary Pincus zachary.pin...@yale.edu
 a[x,y,:]
 
 Read the slicing part of the tutorial:
 http://www.scipy.org/Tentative_NumPy_Tutorial
 (section 1.6)
 
 And the documentation:
 http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
 
 
 
 On Jan 30, 2012, at 10:25 AM, Ted To wrote:
 
  Hi,
 
  Is there some straightforward way to access an array by values across a
  subset of its dimensions?  For example, if I have a three dimensional
  array a=(x,y,z), can I look at the values of z given particular values
  for x and y?
 
  Thanks,
  Ted
  ___
  NumPy-Discussion mailing list
  NumPy-Discussion@scipy.org
  http://mail.scipy.org/mailman/listinfo/numpy-discussion
 
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion
 
 
 
 -- 
 ***
 Chao YUE
 Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
 UMR 1572 CEA-CNRS-UVSQ
 Batiment 712 - Pe 119
 91191 GIF Sur YVETTE Cedex
 Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
 
 
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Addressing arrays

2012-01-30 Thread Ted To
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

On 01/30/2012 10:50 AM, Zachary Pincus wrote:
 Ted, can you clarify what you're asking for? Maybe give a trivial example of 
 an array and the desired output?
 
 I'm pretty sure this is a slicing question though:
 If I have a three dimensional array a=(x,y,z), can I look at the values of z 
 given particular values for x and y?
 Given that element values are scalars in this case, and indices are (x,y,z) 
 triples, it seems likely that looking for values of z given an (x,y) pair 
 is an slicing-by-index question, no?
 
 For indexing-by-value, fancy indexing with boolean masks is usually the way 
 to go... again, Ted (or Chao), if you can describe your indexing needs in a 
 bit more detail, it's often easy to find a compact slicing and/or 
 fancy-indexing strategy that works well and reasonably efficiently.
 
 Zach
 
 
 
 On Jan 30, 2012, at 10:33 AM, Chao YUE wrote:
 
 he is not asking for slicing. he is asking for how to index array by element 
 value but not element index.

 2012/1/30 Zachary Pincus zachary.pin...@yale.edu
 a[x,y,:]

 Read the slicing part of the tutorial:
 http://www.scipy.org/Tentative_NumPy_Tutorial
 (section 1.6)

 And the documentation:
 http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html



 On Jan 30, 2012, at 10:25 AM, Ted To wrote:

 Hi,

 Is there some straightforward way to access an array by values across a
 subset of its dimensions?  For example, if I have a three dimensional
 array a=(x,y,z), can I look at the values of z given particular values
 for x and y?

 Thanks,
 Ted
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion



 -- 
 ***
 Chao YUE
 Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
 UMR 1572 CEA-CNRS-UVSQ
 Batiment 712 - Pe 119
 91191 GIF Sur YVETTE Cedex
 Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
 

 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion
 
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Addressing arrays

2012-01-30 Thread Brett Olsen
On Mon, Jan 30, 2012 at 10:57 AM, Ted To rainexpec...@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
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Addressing arrays

2012-01-30 Thread Ted To
On 01/30/2012 12:13 PM, Brett Olsen wrote:
 On Mon, Jan 30, 2012 at 10:57 AM, Ted To rainexpec...@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
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Addressing arrays

2012-01-30 Thread Zachary Pincus
 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.

I think that your best bet is to form the boolean masks independently and then 
logical-and them together:

mask = (a['month'] == 1)  (a['year'] == 1960)
jan_60 = a[mask]

Someone might have more insight here. Though I should note that if you have 
large data and are doing lots of queries like this, a more database-ish 
approach might be better. Something like sqlite's python bindings, or PyTables. 
Alternately, if your data are all time-series based things, PANDAS might be 
worth looking at. 

But the above approach should be just fine for non-huge datasets...

Zach
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Addressing arrays

2012-01-30 Thread Brett Olsen
On Mon, Jan 30, 2012 at 11:31 AM, Ted To rainexpec...@theo.to wrote:
 On 01/30/2012 12:13 PM, Brett Olsen wrote:
 On Mon, Jan 30, 2012 at 10:57 AM, Ted To rainexpec...@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
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Addressing arrays

2012-01-30 Thread Ted To
 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

Zach and Brett,

Many thanks -- that is exactly what I need.

Cheers,
Ted
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion