Difference between colon [:,:,0] and [...,0] in numpy array

First randomly generate a numpy array of [3,4,5]. Then the x dimension is 3, the shape is (3, 4, 5), which contains a total of 60 elements.

x[:,:,0] means slicing the array x, which can be imagined as a cube of data, and the data of one face is cut each time. If the second dimension is 0, an array of size [3,4] is obtained, that is,

 Can verify:

So what does [...,0] represent?

First of all... can only appear once, which means you can, [ : , : , : ], but [ ... , ...] will report an error.

After using ..., the number 0 is no longer the index of the element, but the axis. The following is specified by numpy.amax() (select the element with the largest axis).

 x size is (3,4,5)

First look at axis=2, the first number 189 is compared from x[ ][ ] [0] to x[ ][ ] [4] , so there are 3*4=12 elements in total

axis=1, the first number 99 is compared from x[ ] [0] [ ] to x[ ] [3] [ ], so there are 3*5=15 elements in total

Similarly, axis=0, the second number 189 is compared from x [0] [ ] [ ] to x [2] [ ] [ ], so there are 4*5=20 elements in total

Schematic diagram of comparison when axis=0:

The most intuitive: the value of the axis selected by the function indicates the number of squares of x[ ][ ][ ], starting from 0, representing the first [ ], that is, x[ ] [ ] [ ], so the dimension The corresponding relationship with axis is that for an array of dimension (3,4,5), axis=0 has a length of 3, axis=1 has a length of 4, and axis=2 has a length of 5.

Then [...,0] means, which is equivalent to [:,:,0]:

At the same time, it can also be used.

Guess you like

Origin blog.csdn.net/qq_39237205/article/details/124201701