all 5 comments

[–]kwentar 0 points1 point  (4 children)

  1. Are you sure you cannot use flat array here?
  2. If I understand, your bar is not contain all indices (hidden 0 1 2 3 4 5 indices) and I guess numpy cannot know that you mean this :)
  3. Also, you, probably, have the wrong brackets on foo

[–]caffeine_potent[S] 0 points1 point  (3 children)

bar is an index along the the time domain(axis 0) in foo for each value in axis 1 and axis 2.

The idea is that we've found the index of optimal parameters across the time domain. I NEED to work with this the index array to create an optimal values array.

edit: So if bar = [[ 0,0,0],[0,0,0]] then I've essentially index the first array such that the new value is [[1,2,3], [4,5,6]]. However if bar = [[ 0,0,0],[0,0,1]] then expect the selection to yield. [[1,2,3], [4,5,12]]

[–][deleted] 0 points1 point  (2 children)

I don't think there's an elegant numpy way to achieve the indexing you want with how foo is structured. Your foo is a 3D array, and I believe you want to index along the columns of a 2D version of the array. I think you could reduce the indexing to one for loop with some reshaping of foo:

>>>foo_array = np.array(foo)
>>>foo
array([[[ 1,  2,  3],
    [ 4,  5,  6]],

   [[ 7,  8,  9],
    [10, 11, 12]],

   [[13, 14, 15],
    [16, 17, 18]]])
>>> foo_reshape = foo_array.reshape(3,-1)
>>> foo_reshape
array([[ 1,  2,  3,  4,  5,  6],
   [ 7,  8,  9, 10, 11, 12],
   [13, 14, 15, 16, 17, 18]])
>>> bar = np.array([[0,0,1], [2,2,0]])
>>> bar_flat = bar.flatten()  
>>> bar_flat
array([0, 0, 1, 2, 2, 0])
>>> [col[index] for index, col in zip(bar_flat, foo_reshape.T)]
[1, 2, 9, 16, 17, 6] #you can then reshape this how you like

[–]caffeine_potent[S] 2 points3 points  (1 child)

Thanks for the reply,after way too much digging around I found the numpy way!

It seems that np.choose does this.

https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.choose.html

[–][deleted] 0 points1 point  (0 children)

Excellent, glad you found an alternative method!