Apr-08-2022, 05:54 PM
(This post was last modified: Apr-08-2022, 05:54 PM by deanhystad.)
This:
Here I make a 4D array with a shape(2, 3, 4, 5).
x[0][1] is a 2D arrays with a shape(4, 5).
x[1][2][3] is a 1D array with shape(5)
x[1][2][3][4] is a scalar that equals 119
Each of these are a slice, a part of the complete array that is sliced away. I can do the same thing with regular lists in Python, but Numpy has a super ginsu knife slicer. I can make arbitrary slices that don't line up with any of the dimensions in Numpy:
Your example: x = output2[0, :, :, i] is making a 2D slice where the index of the first dimension is 0 and the index of the last dimension is i. I cannot say what kind of information this slice contains without knowing the contents of 4D array.
x = output2[0, :, :, i]is called slicing. It "slices" a 2D array out of a 4D array. I will try to explain with an example.
Here I make a 4D array with a shape(2, 3, 4, 5).
import numpy as np x = np.array(range(120)).reshape(2, 3, 4, 5)x[0] and x[1] are 3D arrays, each with a shape(3, 4, 5)
x[0][1] is a 2D arrays with a shape(4, 5).
x[1][2][3] is a 1D array with shape(5)
x[1][2][3][4] is a scalar that equals 119
Each of these are a slice, a part of the complete array that is sliced away. I can do the same thing with regular lists in Python, but Numpy has a super ginsu knife slicer. I can make arbitrary slices that don't line up with any of the dimensions in Numpy:
print(x[0].shape) print(x[0, 1].shape) print(x[1, 2, 3].shape) print(x[1, 2, 3, 4].shape, x[1, 2, 3, 4])
Output:(3, 4, 5)
(4, 5)
(5,)
() 119
Numpy also lets me slice crosswise through multiple dimensions. This is where the ":" comes in. Think of ":" as a wildcard.print(x[:, 0, 0, 0]) print(x[0, :, 0, 0]) print(x[0, 0, :, 0]) print(x[0, 0, 0, :])
Output:[ 0 60]
[ 0 20 40]
[ 0 5 10 15]
[0 1 2 3 4]
x[:, 0, 0, 0] returns a len 2 array because the first dimension is size 1. x[0, :, 0, 0] is len 3, x[0, 0, :, 0] len 4 and x[0, 0, 0, :] len 5.Your example: x = output2[0, :, :, i] is making a 2D slice where the index of the first dimension is 0 and the index of the last dimension is i. I cannot say what kind of information this slice contains without knowing the contents of 4D array.