Second, NumPy base: Array modification

Source: Internet
Author: User

One, array properties
    • Dimension:. Ndim, returning the current array dimension
    • Type:. Dtype, which returns the data type of the elements in the array, note: The array data type defined by NumPy is uniform and cannot be mixed in multiple types
    • Shape:. Shape, returns the shape of the data, the number of elements in each layer of the array
    • Total number of elements:. Size, returns the total number of elements in the array
    • Byte size:. ItemSize, which returns the byte size of each element in the array.
Import NumPy as Npa=np.full ((2,5,4), print ("Array A:", a) print ("Total number of elements in array A:", a.size) print ("Shape of array A:", A.shape) print (" Element type in array a: ", A.dtype) print (" Dimension of Array A: ", A.ndim) print (" element byte size in array A: ", a.itemsize) >>> array A: [[[[]  [[Ten]] [[All] [] [] [] [all]] [ten]] [ten] [all] [] [[All]] [all] [all] ten  10]
   
    [10  [10 10 10 10]] The total number of elements in array A: 40 The shape of the array A: (2, 5, 4) The element type in array A: The dimension of Int32 array A: 3 element in array a byte size: 4
   
Ii. Types of data
    • When not specified, NumPy automatically infers the appropriate data type, so it is generally not necessary to display the given data type.
    • Numeric Dtype are named by a type name (Eg:int, float, and so on) followed by a number that represents the length of each element
    • such as the float data type of python (double-precision floating-point value), takes 8 bytes (64 bits), so it is recorded as float64 in NumPy
    • Each data type has a type code, that is, the shorthand method
Data type Shorthand Description Bytes Range
Integral type Int_ Default Shaping
Intc Equivalent to Long's shaping
int8 I1 byte shaping 1 [-128,127]
Int16 I2 Plastic 2 [ -32768,32767]
Int32 i3 Plastic 4 [ -2^31, 2^31-1]
Int64 I4 Plastic 6 [ -2^63, 2^63-1]
No sign
Integral type
Uint8 U1 No-sign shaping 1 [0,255]
UInt16 U2 No-sign shaping 2 [0,65535]
UInt32 U3 No-sign shaping 1 [0, 2^32-1]
UInt64 U4 No-sign shaping 1 [0,2^64-1]
Boolean Bool_ Boolean value 1 True or False
Floating point Type Float_ Float64 Shorthand Form 8
Float16 F2 Semi-precision floating point type 2 1 Sign bit + 5-bit index + 10-bit decimal part
Float32 F4 or f Single-precision floating-point type 4 1 Sign bit + 8-bit index + 23-bit decimal part
Float64 F8 or d Double-precision floating-point type 8 1 sign bit + 11-bit index + 52-bit decimal part
Plural Complex_ C16 shorthand form of complex128
Complex64 C8 A complex number, represented by two 32-bit floating-point numbers 32
complex128 C16 A complex number, represented by two 64-bit floating-point numbers 64
Object Object O Python Object Type
String String_ S Fixed-length string type (1 bytes per character)
Unicode_ U string of fixed-length Unicode type

Third, array modification (attributes)
    • Shape modification:. Reshape (),. T
    • Dimension modification:. Reshape ()
    • Type modification:. Astype ()
3.1, Reshape () method: Modify array shape and dimension
    • Directly modifies the shape value of the array ndarray, requiring the product to be unchanged after modification.
    • Use the reshape function directly to create a new array that changes size, the shape of the original array remains the same, but the new array and the original array share a memory space , that is, modifying the values in any one array will affect the other. Additionally, the number of elements in the new array is required to match the original array.
    • When you specify an axis of 1, the length value of the axis is automatically calculated based on the number of elements in the array.
Import NumPy as Npa=np.arange print ("Original array (one dimension):", a) B=a.reshape (2,3,5) print ("New Array (three-dimensional):", b) B[1][2][2]=666print (" Modifying the values in B affects the A: ", a) print (" Use dimension strike against B: ", B.reshape (10,-1)) >>> original array (one dimension): [0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29] New Array (three-dimensional): [[[0] 1 2  3  4]< c13/>[5  6  7  8  9]  [  [All  ] [[] [[] [] [25 26 27 28 29] ]] Modify the values in B to affect a: [  0   1   2   3   4 5 6   7   8 9-  12 For all the above,  the  666  29] Use a reduced-dimension strike on B: [[  0   1   2] [  3 4 5   ] [  6   7 8   ] [  9 [[] [] [[] [] [] [] []  C71/>26] [666  29]]
3.2. T method: Row and column transpose
    • After using the T method, you can transpose the rows and columns of the array.
Import NumPy as Npa=np.random.randint (1,10,size= (2,3,4)) B=a.tprint (B.shape) >>> (4, 3, 2)
3.2. Astype () Method: Modify the element type
    • If you need to change the data type of an already existing array, you can modify the Astype method to get a new array.
Import NumPy as Npa=np.arange (1,5) print ("original array: {0} with data type: {1}". Format (A,a.dtype)) B=a.astype (float) print ("new array: {0}, Its data type is: {1} ". Format (B,b.dtype)) >>> original array: [1 2 3 4], whose data type is: Int32 new array: [1. 2.3. 4.], whose data type is: float64

Second, NumPy base: Array modification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.