Get the App
SLTechnology News&Howtos  ›  Development  › 

The meaning of colons and minus signs in numpy arrays

Shulou Source: shulou.com Published: 2022-06-03 18:00:28 09月30日 Update

This article mainly explains "the meaning of colons and minus signs in numpy array". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the meaning of colons and minus signs in the numpy array.

When actually using numpy, we often use the-1 dimension and ":" of the numpy array to call elements in the numpy array. I am also often confused by the dimensions of the array.

In general, ":" is used to represent all submodules of the current dimension

"- 1" is used to indicate the last of all sub-modules in the current dimension, and "negative sign is used to indicate elements counted from back to front."

Test code

Import numpy as npb = np.arange (start=0, stop=24, dtype=int) print ('b.shapestones, b.shape) # b.shape (24,) b1 = b.reshape ((4,2,3) print (' the value of b1\ n') B1) # the value of bundles [0 12] # [3 45]] # # [[6 7 8] # [9 10 11]] # [[12 13 14] # [15 16 17]] # [[18 19 20] # [21 22 23] print ('b1 [- 1]\ n' B1 [- 1]) # decompose the last module from the outermost dimension # b1 [- 1] # [[18 19 20] # [21 22 23]] for an in b1 [- 1]: print ('s') # there are two small modules in this module So the program runs # s # sprint ('b1 [:-1]\ n 'twice B1 [:-1]) # the remaining module # b1 [:-1] # [0 12] # [3 45]] # [[6 7 8] # [9 10 11]] # [[12 13 14] # [15 16 17] for A1 in b1 [:-1]: print ('s') # has three small modules in this module So the program runs twice # s # s # sprint ('b1 [- 1:]\ nkeeper, b1 [- 1:]) # is written in the ":" of the last dimension has no substantive effect The meaning expressed here is the same as b1 [- 1] # b1 [- 1:] # [18 19 20] # [21 22 23] print ('b1 [:,-1]\ ndimension, b1 [:,-1]) # indicates that the last submodule # b1 [is selected in each submodule after all the outermost dimensions are taken out: -1] # [[3 45] # [9 10 11] # [15 16 17] # [21 22 23]] print ('b1 [:,-1]\ nB1 [:,:,-1]) # indicates that the last element of the innermost dimension is taken to form a new tuple # b1 [:,-1] # [[25] # [8 11] # [14 17] # [20 23] I believe you have a deeper understanding of "the meaning of colons and minus signs in the numpy array". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Module dimension array negative sign for colon meaning size element outer layer content actual program learning running practical deeper same confused three Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL vpn Linux Redmi NVidia