Re: [Numpy-discussion] Extracting values from one array corresponding to argmax elements in another array

2010-04-09 Thread Friedrich Romstedt
2010/4/5 Ken Basye kbas...@jhu.edu:
  I have two arrays, A and B, with the same shape.  I want to find the
 highest values in A along some axis, then extract the corresponding
 values from B.

Maybe:

def select(A, B, axis):
# Extract incomplete index tuples:
argmax = a.argmax(axis = axis)

# Create the selection list to be handed over to B.__getitem__() ...

advanced_index = []

# Decompose the index tuples.
for dimi in xrange(argmax.shape[1]):
advanced_index.append(argmax[:, dimi])

# Insert the missing dimension.
advanced_index.insert(axis, numpy.arange(0, B.shape[axis]))

# Perform advanced (integer) selection ...

return B[advanced_index]

 a
array([[[0, 1],
[2, 3]],

   [[4, 5],
[6, 7]]])
 select(a, a, 0)
array([3, 7])
 select(a, a, 1)
array([5, 7])
 select(a, a, 2)
array([6, 7])

It seems to work.

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


Re: [Numpy-discussion] Extracting values from one array corresponding to argmax elements in another array

2010-04-07 Thread Vincent Schut
On 04/06/2010 03:22 PM, Ken Basye wrote:
 From: Vincent Schut sc...@sarvision.nl
 On 04/05/2010 06:06 PM, Keith Goodman wrote:

 On Mon, Apr 5, 2010 at 8:44 AM, Ken Basyekbas...@jhu.edu   wrote:

snip
 b[a.argmax(axis=0), range(3)]

  array([0, 4, 5])


 Which does not work anymore when your arrays become more-dimensional
 (like in my case: 4 or more) and the axis you want to select on is not
 the first/last one. If I recall correctly, I needed to construct the
 full index arrays for the other dimensions too (like with ogrid I
 think). So: create the ogrid, replace the one for the dimensions you
 want the argmax selection to take place on with the argmax parameter,
 and use those index arrays to index your b array.
 I'd need to look up my source code to be more sure/precise. If anyone
 would like me to, please let me know. If anyone knows a less elaborate
 way, also please let us know! :-)

 Hi Vincent,
 I'd like to see more about your solution. For my present purposes,
 Keith's solution was sufficient, but I'm still very interested in a
 solution that's independent of dimension and axis.
 Thanks (and thanks, Keith),
 Ken


I've tracked it down. I have created the following quick'n dirty function:

def selectFromDimension(data, selection, dimension):
 ogridStatement = numpy.ogrid[
 slices = []
 for d in data.shape:
 slices.append(0: + str(d))
 ogridStatement += ,.join(slices)
 ogridStatement += ]
 grid = eval(ogridStatement)
 grid[dimension] = selection
 result = data[grid]
 return result

which you call with the array to select from, the result of 
argmin/max(axis=...), and the axis to select from (usually the same as 
the axis argument to the argmin/max)

It uses eval to be able to create the ogrid independent of 
dimensionality. There's probably a much cleaner way, but this worked for 
me :-)

Now I think about it, probably another way could be to transpose your 
axes such that the axis to select on becomes the first axis. Then just 
index with the result of argmin/max, and transpose back if necessary. 
However, I have not tried this and I might miss something obvious here...

Vincent.

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


Re: [Numpy-discussion] Extracting values from one array corresponding to argmax elements in another array

2010-04-06 Thread Vincent Schut
On 04/05/2010 06:06 PM, Keith Goodman wrote:
 On Mon, Apr 5, 2010 at 8:44 AM, Ken Basyekbas...@jhu.edu  wrote:
 Hi Folks,
   I have two arrays, A and B, with the same shape.  I want to find the
 highest values in A along some axis, then extract the corresponding
 values from B.  I can get the highest values in A with A.max(axis=0) and
 the indices of these highest values with A.argmax(axis=0).  I'm trying
 to figure out a loop-free way to extract the corresponding elements from
 B using these indices.  Here's code with a loop that will do what I want
 for two-dimensional arrays:

 a
 array([[ 100.,0.,0.],
[   0.,  100.,  100.],
[   0.,0.,0.]])

 a.max(axis=0)
 array([ 100.,  100.,  100.])

 sel = a.argmax(axis=0)
   sel
 array([0, 1, 1])

 b = np.arange(9).reshape((3,3))
 b
 array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

 b_best = np.empty(3)
 for i in xrange(3):
 ...b_best[i] = b[sel[i], i]
 ...
 b_best
 array([ 0.,  4.,  5.])

 Here's one way:

 b[a.argmax(axis=0), range(3)]
 array([0, 4, 5])

Which does not work anymore when your arrays become more-dimensional 
(like in my case: 4 or more) and the axis you want to select on is not 
the first/last one. If I recall correctly, I needed to construct the 
full index arrays for the other dimensions too (like with ogrid I 
think). So: create the ogrid, replace the one for the dimensions you 
want the argmax selection to take place on with the argmax parameter, 
and use those index arrays to index your b array.
I'd need to look up my source code to be more sure/precise. If anyone 
would like me to, please let me know. If anyone knows a less elaborate 
way, also please let us know! :-)

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


Re: [Numpy-discussion] Extracting values from one array corresponding to argmax elements in another array

2010-04-06 Thread Ken Basye

From: Vincent Schut sc...@sarvision.nl

On 04/05/2010 06:06 PM, Keith Goodman wrote:
  

On Mon, Apr 5, 2010 at 8:44 AM, Ken Basyekbas...@jhu.edu  wrote:


Hi Folks,
  I have two arrays, A and B, with the same shape.  I want to find the
highest values in A along some axis, then extract the corresponding
values from B.  I can get the highest values in A with A.max(axis=0) and
the indices of these highest values with A.argmax(axis=0).  I'm trying
to figure out a loop-free way to extract the corresponding elements from
B using these indices.  Here's code with a loop that will do what I want
for two-dimensional arrays:

a
array([[ 100.,0.,0.],
   [   0.,  100.,  100.],
   [   0.,0.,0.]])

a.max(axis=0)
array([ 100.,  100.,  100.])

sel = a.argmax(axis=0)
  sel
array([0, 1, 1])

b = np.arange(9).reshape((3,3))
b
array([[0, 1, 2],
   [3, 4, 5],
   [6, 7, 8]])

b_best = np.empty(3)
for i in xrange(3):
...b_best[i] = b[sel[i], i]
...
b_best
array([ 0.,  4.,  5.])
  

Here's one way:



b[a.argmax(axis=0), range(3)]


array([0, 4, 5])



Which does not work anymore when your arrays become more-dimensional 
(like in my case: 4 or more) and the axis you want to select on is not 
the first/last one. If I recall correctly, I needed to construct the 
full index arrays for the other dimensions too (like with ogrid I 
think). So: create the ogrid, replace the one for the dimensions you 
want the argmax selection to take place on with the argmax parameter, 
and use those index arrays to index your b array.
I'd need to look up my source code to be more sure/precise. If anyone 
would like me to, please let me know. If anyone knows a less elaborate 
way, also please let us know! :-)
  

Hi Vincent,
 I'd like to see more about your solution.  For my present purposes, 
Keith's solution was sufficient, but I'm still very interested in a 
solution that's independent of dimension and axis. 
 Thanks (and thanks, Keith),

Ken

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


Re: [Numpy-discussion] Extracting values from one array corresponding to argmax elements in another array

2010-04-06 Thread josef . pktd
On Tue, Apr 6, 2010 at 9:22 AM, Ken Basye kbas...@jhu.edu wrote:
 From: Vincent Schut sc...@sarvision.nl

 On 04/05/2010 06:06 PM, Keith Goodman wrote:


 On Mon, Apr 5, 2010 at 8:44 AM, Ken Basyekbas...@jhu.edu  wrote:


 Hi Folks,
   I have two arrays, A and B, with the same shape.  I want to find the
 highest values in A along some axis, then extract the corresponding
 values from B.  I can get the highest values in A with A.max(axis=0) and
 the indices of these highest values with A.argmax(axis=0).  I'm trying
 to figure out a loop-free way to extract the corresponding elements from
 B using these indices.  Here's code with a loop that will do what I want
 for two-dimensional arrays:

 a
 array([[ 100.,0.,0.],
[   0.,  100.,  100.],
[   0.,0.,0.]])

 a.max(axis=0)
 array([ 100.,  100.,  100.])

 sel = a.argmax(axis=0)
   sel
 array([0, 1, 1])

 b = np.arange(9).reshape((3,3))
 b
 array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

 b_best = np.empty(3)
 for i in xrange(3):
 ...b_best[i] = b[sel[i], i]
 ...
 b_best
 array([ 0.,  4.,  5.])


 Here's one way:



 b[a.argmax(axis=0), range(3)]


 array([0, 4, 5])


 Which does not work anymore when your arrays become more-dimensional
 (like in my case: 4 or more) and the axis you want to select on is not
 the first/last one. If I recall correctly, I needed to construct the
 full index arrays for the other dimensions too (like with ogrid I
 think). So: create the ogrid, replace the one for the dimensions you
 want the argmax selection to take place on with the argmax parameter,
 and use those index arrays to index your b array.
 I'd need to look up my source code to be more sure/precise. If anyone
 would like me to, please let me know. If anyone knows a less elaborate
 way, also please let us know! :-)


 Hi Vincent,
   I'd like to see more about your solution.  For my present purposes,
 Keith's solution was sufficient, but I'm still very interested in a solution
 that's independent of dimension and axis.
   Thanks (and thanks, Keith),
  Ken

an alternative to Vincent's general solution, if you have unique max
or want all argmax is using a mask

 a
array([[ 100.,0.,0.],
   [   0.,  100.,  100.],
   [   0.,0.,0.]])
 b
array([[0, 1, 2],
   [3, 4, 5],
   [6, 7, 8]])

 ax=0; b[a==np.expand_dims(a.max(ax),ax)]
array([0, 4, 5])
 ax=1; b[a==np.expand_dims(a.max(ax),ax)]
array([0, 4, 5, 6, 7, 8])

 aa=np.eye(3)
 ax=1; b[aa==np.expand_dims(aa.max(ax),ax)]
array([0, 4, 8])
 ax=0; b[aa==np.expand_dims(aa.max(ax),ax)]
array([0, 4, 8])

Josef


 ___
 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] Extracting values from one array corresponding to argmax elements in another array

2010-04-05 Thread Ken Basye
Hi Folks,
  I have two arrays, A and B, with the same shape.  I want to find the 
highest values in A along some axis, then extract the corresponding 
values from B.  I can get the highest values in A with A.max(axis=0) and 
the indices of these highest values with A.argmax(axis=0).  I'm trying 
to figure out a loop-free way to extract the corresponding elements from 
B using these indices.  Here's code with a loop that will do what I want 
for two-dimensional arrays:

  a
array([[ 100.,0.,0.],
   [   0.,  100.,  100.],
   [   0.,0.,0.]])

  a.max(axis=0)
array([ 100.,  100.,  100.])

  sel = a.argmax(axis=0)
 sel
array([0, 1, 1])

  b = np.arange(9).reshape((3,3))
  b
array([[0, 1, 2],
   [3, 4, 5],
   [6, 7, 8]])

  b_best = np.empty(3)
  for i in xrange(3):
...b_best[i] = b[sel[i], i]
...
  b_best
array([ 0.,  4.,  5.])

I tried several approaches with take() but now that I understand how 
take() works when you give it an axis argument it seems like this isn't 
going to do what I want.  Still, it seems like there should be some 
shortcut...

TIA,
   Ken

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


Re: [Numpy-discussion] Extracting values from one array corresponding to argmax elements in another array

2010-04-05 Thread Keith Goodman
On Mon, Apr 5, 2010 at 8:44 AM, Ken Basye kbas...@jhu.edu wrote:
 Hi Folks,
  I have two arrays, A and B, with the same shape.  I want to find the
 highest values in A along some axis, then extract the corresponding
 values from B.  I can get the highest values in A with A.max(axis=0) and
 the indices of these highest values with A.argmax(axis=0).  I'm trying
 to figure out a loop-free way to extract the corresponding elements from
 B using these indices.  Here's code with a loop that will do what I want
 for two-dimensional arrays:

   a
 array([[ 100.,    0.,    0.],
       [   0.,  100.,  100.],
       [   0.,    0.,    0.]])

   a.max(axis=0)
 array([ 100.,  100.,  100.])

   sel = a.argmax(axis=0)
  sel
 array([0, 1, 1])

   b = np.arange(9).reshape((3,3))
   b
 array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

   b_best = np.empty(3)
   for i in xrange(3):
 ...    b_best[i] = b[sel[i], i]
 ...
   b_best
 array([ 0.,  4.,  5.])

Here's one way:

 b[a.argmax(axis=0), range(3)]
   array([0, 4, 5])
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion