Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain this 4D numpy array indexing intuitively

x = np.random.randn(4, 3, 3, 2)
print(x[1,1])

output:
[[ 1.68158825 -0.03701415]
[ 1.0907524  -1.94530359]
[ 0.25659178  0.00475093]]

I am python newbie. I can't really understand 4-D array index like above. What does x[1,1] mean?

For example, for vector

a = [[2][3][8][9]], a[0] = 2, a[3] = 9. 

I get this but I don't know what x[1,1] refers to.

Please explain in detail. Thank you.

like image 250
RPM Avatar asked Nov 06 '17 18:11

RPM


People also ask

What is NumPy array indexing?

Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

What does a 4D array mean?

Prerequisite :Array in C/C++, More on array A four-dimensional (4D) array is an array of array of arrays of arrays or in other words 4D array is a array of 3D array. More dimensions in an array means more data be held, but also means greater difficulty in managing and understanding arrays.

What is NumPy array explain with the help of indexing and slicing operations?

Slicing arrays Slicing in python means taking elements from one given index to another given index. We pass slice instead of index like this: [start:end] . We can also define the step, like this: [start:end:step] .

What are 4D arrays used for?

A practical use of a 4D array is to keep track of a 3d object, could keep track of [x-cord][y-cord][z-cord][time]. This 4D array would be a useful use of a 4D array. This could keep track of a range of cords and time, and the value in the array could say the speed of of the object.


1 Answers

A 2D array is a matrix : an array of arrays.

A 4D array is basically a matrix of matrices:

matrix of matrices

Specifying one index gives you an array of matrices:

>>> x[1]
array([[[-0.37387191, -0.19582887],
        [-2.88810217, -0.8249608 ],
        [-0.46763329,  1.18628611]],

       [[-1.52766397, -0.2922034 ],
        [ 0.27643125, -0.87816021],
        [-0.49936658,  0.84011388]],

       [[ 0.41885001,  0.16037164],
        [ 1.21510322,  0.01923682],
        [ 0.96039904, -0.22761806]]])

enter image description here

Specifying two indices gives you a matrix:

>>> x[1, 1]
array([[-1.52766397, -0.2922034 ],
       [ 0.27643125, -0.87816021],
       [-0.49936658,  0.84011388]])

enter image description here

Specifying three indices gives you an array:

>>> x[1, 1, 1]
array([ 0.27643125, -0.87816021])

enter image description here

Specifying four indices gives you a single element:

>>> x[1, 1, 1, 1]
-0.87816021212791107

enter image description here

x[1,1] gives you the small matrix that was saved in the 2nd column of the 2nd row of the large matrix.

like image 118
Eric Duminil Avatar answered Oct 06 '22 00:10

Eric Duminil