Indexing a python numpy array. How it works?

I stumbled upon this python code (which works) and for me it seems awesome. However, I cannot understand what this code does. To reproduce it, I kind of wrote a test code:

import numpy as np

# Create a random array which represent the 6 unique coeff. 
# of a symmetric 3x3 matrix
x = np.random.rand(10, 10, 6)

So, I have 100 symmetric 3x3 matrices, and I only store unique components. Now I want to generate a full 3x3 matrix, and that is where the magic happens.

indices = np.array([[0, 1, 3],
                    [1, 2, 4],
                    [3, 4, 5]])

I see it doing this. Since the components of the index 0-5 must be located in the 3x3 matrix in order to have a symmetric matrix.

mat = x[..., indices]

This line has lost me. Thus, he is working on the last measurement of the x array, but it’s completely not clear to me how the permutation and rebuilding are done, but it really returns an array of the form (10, 10, 3, 3). I am amazed and confused!

+4
1

- bi rico.

, x.shape - (10,20,30), ind - (2,3,4) - intp, thenresult = x [..., ind,:] (10,2, 3,4,30), (20,) (2,3,4) - . i, j, kloop (2,3,4) - , [..., i, j, k,:] = x [..., ind [i, j, k],:]. , x.take(ind, axis = -2).

+1

Source: https://habr.com/ru/post/1570523/


All Articles