1

I want to get the values at indices of my_array.

indices = np.array([[[0],
        [1],
        [0]]])

my_array = np.array([[[1.1587323 , 1.75406635],
        [1.05464125, 1.29215026],
        [0.9784655 , 1.16957462]]])

I should get the following output:

output: array([[[1.1587323], [1.29215026], [0.9784655]]])

Is it possible without for loops or list comprehensions?

1 Answers1

2

You can use np.take_along_axis:

np.take_along_axis(my_array, indices, axis=-1)
array([[[1.1587323 ],
        [1.29215026],
        [0.9784655 ]]])
yatu
  • 86,083
  • 12
  • 84
  • 139