0

I'm trying to understand what the following does at a conceptual level. Let's say we have two numpy arrays of random integers

arr1

array([[2, 2, 2, 2, 1],
       [1, 3, 1, 3, 2],
       [2, 2, 2, 1, 3],
       [1, 1, 1, 3, 2]])

arr2

array([[1, 3, 1, 1, 3, 3, 2, 2],
       [2, 3, 2, 2, 2, 3, 2, 1],
       [3, 3, 3, 1, 1, 3, 3, 3],
       [1, 1, 2, 1, 2, 1, 1, 1]])

Then, I do a nested indexing of the second array arr2 into the first one arr1, obtaining

arr1[arr2,:]

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

       [[2, 2, 2, 1, 3],
        [1, 1, 1, 3, 2],
        [2, 2, 2, 1, 3],
        [2, 2, 2, 1, 3],
        [2, 2, 2, 1, 3],
        [1, 1, 1, 3, 2],
        [2, 2, 2, 1, 3],
        [1, 3, 1, 3, 2]],

       [[1, 1, 1, 3, 2],
        [1, 1, 1, 3, 2],
        [1, 1, 1, 3, 2],
        [1, 3, 1, 3, 2],
        [1, 3, 1, 3, 2],
        [1, 1, 1, 3, 2],
        [1, 1, 1, 3, 2],
        [1, 1, 1, 3, 2]],

       [[1, 3, 1, 3, 2],
        [1, 3, 1, 3, 2],
        [2, 2, 2, 1, 3],
        [1, 3, 1, 3, 2],
        [2, 2, 2, 1, 3],
        [1, 3, 1, 3, 2],
        [1, 3, 1, 3, 2],
        [1, 3, 1, 3, 2]]])

which is a new array with shape (4,8,5). It is not clear to me how should I interpret this new object, and how the entries of the two arrays are actually combined together.

1
  • 1
    One way of thinking of this operation is that you "replaced" each element of arr2 with the corresponding row of `arr1'. Hence the (4,8) shape has expanded to (4,8,5). Commented Jan 11, 2023 at 17:11

1 Answer 1

1

Reference on numpy ndarray indexing with integer arrays


TLDR:

out = arr1[arr2, :]
out[i, j, k] == arr1[ arr2[i, j], k ] # for all valid indices i,j,k 

Intuition:

The values inside arr2 are being used independently/separately to index the first axis of arr1, and the results are placed into a new array with the same shape as arr2.

Sign up to request clarification or add additional context in comments.

2 Comments

much clearer now, but why the shape has an extra dimension?
Because arr2 has shape (4, 8), so it defines a subspace with that same shape. The third dimension is resulting from the slice that is used to index the other axis in arr1 (with shape (5,). Together the result is shape (4, 8, 5).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.