[Reprint] [Reprint] Getting started with Numpy library in python

Reference link: numpy.ones_like in Python

Reference link: numpy.full_like in Python 

table of Contents 

 N-dimensional array objects: ndarray examples ndarray object attributes ndarray array element type ndarray array creation method ndarray array creation method from lists, tuples and other types in Python to create ndarray array using NumPy functions 

     Dimension transformation of ndarray array ndarray type transformation ndarray array to list conversion 

    ndarray array operation index and slice of array 

    Operation of ndarray array NumPy unary function NumPy binary function 

    

   

  

  

  

N-dimensional array object: ndarray  

Reference  

>>> import numpy as np 

>>> def npSum(): 

    a = np.array([0, 1, 2, 3, 4]) 

    b = np.array([9, 8, 7, 6, 5]) 

    c = a**2 + b**3 

    return c 

  

>>> print(npSum()) 

[729 513 347 225 141] 

  

Properties of the ndarray object  

Attribute description. ndim rank, that is, the number of axes or the number of dimensions. The scale of the shapendarray object. For a matrix, the number of elements of the .sizendarray object with n rows and m columns is equivalent to the value of n*m in the .shape. The size of each element in the type.itemsizendarray object, in bytes 

>>> import numpy as np 

>>> a = np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) 

>>> a.ndim 

>>> a.shape 

(2, 5) 

>>> a.size 

10 

>>> a.dtype 

dtype('int32') 

>>> a.itemsize 

  

The element type of the ndarray array  

Data type description bool Boolean type, True or Falseintc is consistent with the int type in C language, generally int32 or int64intp is used for indexing integer, consistent with ssize_t in C language, int32 or int64int8 byte length integer, value: 

        

         

          

           

           [ 

           

           

           ‐ 

           

           

           128 

           

           

           , 

           

           

           127 

           

           

           ] 

           

          

          

          [‐128, 127] 

          

         

       [‐128,127] int16 An integer with a length of 16 bits, the value is: 

        

         

          

           

           [ 

           

           

           ‐ 

           

           

           32768 

           

           

           , 

           

           

           32767 

           

           

           ] 

           

          

          

          [‐32768, 32767] 

          

         

       [‐32768,32767]int3232-bit length integer, value: 

        

         

          

           

           [ 

           

           

           ‐ 

           

           

            

            2 

            

            

            31 

            

           

           

           , 

           

           

            

            2 

            

            

            31 

            

           

           

           ‐ 

           

           

           1 

           

           

           ] 

           

          

          

          [‐2^{31}, 2^{31}‐1] 

          

         

       [‐231,231‐1]int6464-bit length integer, value: 

        

         

          

           

           [ 

           

           

           ‐ 

           

           

            

            2 

            

            

            63 

            

           

           

           , 

           

           

            

            2 

            

            

            63 

            

           

           

           ‐ 

           

           

           1 

           

           

           ] 

           

          

          

          [‐2^{63}, 2^{63}‐1] 

          

         

       [‐263,263‐1] uint 88-bit unsigned integer, value: 

        

         

          

           

           [ 

           

           

           0 

           

           

           , 

           

           

           255 

           

           

           ] 

           

          

          

          [0, 255] 

          

         

       [0,255]uint1616-bit unsigned integer, value: 

        

         

          

           

           [ 

           

           

           0 

           

           

           , 

           

           

           65535 

           

           

           ] 

           

          

          

          [0, 65535] 

          

         

       [0,65535] uint3232-bit unsigned integer, value: 

        

         

          

           

           [ 

           

           

           0 

           

           

           , 

           

           

            

            2 

            

            

            32 

            

           

           

           ‐ 

           

           

           1 

           

           

           ] 

           

          

          

          [0, 2^{32}‐1] 

          

         

       [0,232-1] uint6432 bit unsigned integer, value: 

        

         

          

           

           [ 

           

           

           0 

           

           

           , 

           

           

            

            2 

            

            

            64 

            

           

           

           ‐ 

           

           

           1 

           

           

           ] 

           

          

          

          [0, 2^{64}‐1] 

          

         

       [0,264-1] float16 16-bit half-precision floating-point number: 1 sign bit, 5-bit exponent, 10-bit mantissa float3232-bit half-precision floating point number: 1 sign bit, 8-bit exponent, 23-bit mantissa float6464 half-precision floating point number: 1-bit sign bit, 11-bit exponent, 52-bit mantissa complex64 complex number type, real and imaginary parts are both 32-bit floating point numbers complex128 complex number type, real and imaginary parts are both 64-bit floating point numbers 

ndarray array creation method  

Create ndarray arrays from lists, tuples, etc. in Python  

>>> import numpy as np 

>>> x = np.array([ [1,2], [9,8], (0.1,0.2) ]) 

>>> print(x) 

[[1.  2. ] 

 [9.  8. ] 

 [0.1 0.2]] 

  

Use NumPy functions to create an ndarray array  

Function description np.arange(n) is similar to the range() function and returns the ndarray type. The elements are from 0 to n-1 np.ones(shape) generates an array of all 1s according to the shape, and shape is the tuple type np.zeros(shape) according to shape generates an array of all 0s, shape is a tuple type np.full(shape,val) generates an array according to shape, each element value is valnp.eye(n) to create a square n*n identity matrix, diagonal The line is 1, and the rest are 0np.ones_like(a) Generate an array of all 1s based on the shape of array a np.zeros_like(a) Generate an array of all 0s based on the shape of array a np.full_like(a,val) The shape generates an array, each element value is valnp.linspace() fills in the data at equal intervals according to the start and end data to form an array np.concatenate() merges two or more arrays into a new array 

>>> import numpy as np 

>>> print(np.arange(5)) 

[0 1 2 3 4] 

>>> print(np.ones((2,3))) 

[[1. 1. 1.] 

 [1. 1. 1.]] 

>>> print(np.zeros((1,5))) 

[[0. 0. 0. 0. 0.]] 

>>> print(np.full((3,3),8.6)) 

[[8.6 8.6 8.6] 

 [8.6 8.6 8.6] 

 [8.6 8.6 8.6]] 

>>> print(np.eye(3)) 

[[1. 0. 0.] 

 [0. 1. 0.] 

 [0. 0. 1.]] 

>>> a = np.linspace(1, 10, 4) 

>>> a 

array([ 1.,  4.,  7., 10.]) 

>>> b = np.linspace(1, 10, 4, endpoint=False) 

>>> b 

array([1.  , 3.25, 5.5 , 7.75]) 

>>> c = np.concatenate((a,b)) 

>>> c 

array([ 1.  ,  4.  ,  7.  , 10.  ,  1.  ,  3.25,  5.5 ,  7.75]) 

  

Dimension transformation of ndarray array  

Method description. reshape(shape) does not change the array elements, returns an array of shape shape, the original array remains unchanged. resize(shape) has the same function as .reshape(), but modifies the original array. swapaxes(ax1,ax2) changes the array to n Two of the two dimensions are swapped. flatten() reduces the dimensionality of the array and returns the folded one-dimensional array, the original array remains unchanged 

>>> a = np.ones((2,3,4), dtype=np.int32) 

>>> a 

array([[[1, 1, 1, 1], 

        [1, 1, 1, 1], 

        [1, 1, 1, 1]], 

  

       [[1, 1, 1, 1], 

        [1, 1, 1, 1], 

        [1, 1, 1, 1]]]) 

>>> a.reshape(3,8) 

array([[1, 1, 1, 1, 1, 1, 1, 1], 

       [1, 1, 1, 1, 1, 1, 1, 1], 

       [1, 1, 1, 1, 1, 1, 1, 1]]) 

>>> a.resize(3,8) 

>>> a 

array([[1, 1, 1, 1, 1, 1, 1, 1], 

       [1, 1, 1, 1, 1, 1, 1, 1], 

       [1, 1, 1, 1, 1, 1, 1, 1]]) 

>>> a.flatten() 

array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) 

>>> a.swapaxes(0,1) 

array([[1, 1, 1], 

       [1, 1, 1], 

       [1, 1, 1], 

       [1, 1, 1], 

       [1, 1, 1], 

       [1, 1, 1], 

       [1, 1, 1], 

       [1, 1, 1]]) 

  

Type conversion of ndarray array  

new_a = a.astype(new_type)  

Note: The astype() method will definitely create a new array (a copy of the original data), even if the two types are the same  

>>> a = np.ones((2,3,4), dtype=np.int32) 

>>> a 

array([[[1, 1, 1, 1], 

        [1, 1, 1, 1], 

        [1, 1, 1, 1]], 

  

       [[1, 1, 1, 1], 

        [1, 1, 1, 1], 

        [1, 1, 1, 1]]]) 

>>> b = a.astype(np.float) 

>>> b 

array([[[1., 1., 1., 1.], 

        [1., 1., 1., 1.], 

        [1., 1., 1., 1.]], 

  

       [[1., 1., 1., 1.], 

        [1., 1., 1., 1.], 

        [1., 1., 1., 1.]]]) 

  

ndarray array to list conversion  

ls = a.tolist ()  

ndarray array operation  

Index and slice of array  

Indexing and slicing of one-dimensional arrays: similar to Python lists Indexing of multi-dimensional arrays: slices of multi-dimensional array   

ndarray array operations  

NumPy unary function  

Function description np.abs(x) np.fabs(x) calculate the absolute value of each element of the array np.sqrt(x) calculate the square root of each element of the array np.square(x) calculate the square of each element of the array np.log(x ) np.log10(x) np.log2(x) calculate the natural logarithm, base 10 logarithm and base 2 logarithm of each element of the array np.ceil(x) np.floor(x) calculate the ceiling value of each element of the array Or the floor value np.rint(x) calculates the rounding value of each element of the array np.modf(x) returns the decimal and integer parts of each element of the array as two independent arrays np.cos(x) np.cosh(x) np.sin(x) np.sinh(x) np.tan(x) np.tanh(x) calculates the normal and hyperbolic trigonometric functions of each element of the array np.exp(x) calculates the exponent value of each element of the array np.sign(x) calculates the sign value of each element of the array, 

        

         

          

           

           1 

           

           

           ( 

           

           

           + 

           

           

           ) 

           

           

           , 

           

           

           0 

           

           

           , 

           

           

           ‐ 

           

           

           1 

           

           

           ( 

           

           

           ‐ 

           

           

           ) 

           

          

          

          1(+), 0, ‐1(‐) 

          

         

       1(+),0,‐1(‐) 

NumPy binary function  

Function description + ‐ * / ** Each element of the two arrays performs corresponding operations np.maximum(x,y) = np.fmax(); np.minimum(x,y) = np.fmin() element-level maximum /Minimum calculation np.mod(x,y) element-level modular operation np.copysign(x,y) assigns the sign of each element value in the array y to the corresponding element of the array x> <>= <= == != Arithmetic comparison, generating a boolean array

Guess you like

Origin blog.csdn.net/u013946150/article/details/112976704