JavaScript
x
5
1
arr = np.arange(12).reshape((3, 2, 2))
2
indices = np.array([0, 1, 1])
3
4
expected_outcome = np.array([[0, 1], [6, 7], [10, 11]])
5
I’m trying to index this array of shape (3,2,2) with an array of shape (3) containing the y-index of the value I want to get. I tried to make it work with for in
statement, but is there an elegant way to do it with numpy?
Advertisement
Answer
So you want arr[0,0,:], arr[1,1,:], arr[2,1,:]
?
How about
JavaScript
1
6
1
In [179]: arr[[0,1,2], [0,1,1]]
2
Out[179]:
3
array([[ 0, 1],
4
[ 6, 7],
5
[10, 11]])
6