Numpy
NumPy is the foundation package for high-performance scientific computing and data analysis. It is the basis of various other tools such as pandas.
Main functions of NumPy:
Ndarray, a multidimensional array structure, efficient and space-saving
Mathematical functions that do not require a loop to perform fast operations on an entire set of data
Tools for reading and writing disk data, and tools for manipulating memory-mapped files
Linear algebra, random number generation and Fourier transform functions
Tools for integrating code such as C, C + +
Installation method: Pip Install NumPy
Citation method: Import NumPy as NP
Create Ndarray:np.array ()
Application Scenarios:
Example 1: The market value (USD) of several multinational companies is known and converted into renminbi
Example 2: The price of each item in the cart is known and the number of items, the total amount
Ndarray can also be multidimensional arrays, but element types must be the same
Common Properties:
transpose of the T array (for high-dimensional arrays) the data type of the Dtype array element size array element number Ndim the dimension of the array size of the dimension of the shape Array (in the form of tuples)
Dtype
Bool_, Int (8,16,32,64), uint (8,16,32,64), float (16,32,64)
Type conversion: Astype ()
Create Ndarray:
Array () converts the list to an array, optionally specifying Dtype explicitly>>> Np.array ([1, 2, 3]) Array ([1, 2, 3]) Arange () range version numpy, support floating-point numbers>>> Np.arange (3.0) Array ([0.,1., 2.]) Linspace () similar to Arange (), the third parameter is an array length>>> Np.linspace (2.0, 3.0, num=5) Array ([2., 2.25, 2.5, 2.75, 3. ]) zeros () creates a full 0 array based on the specified shape and Dtype>>> Np.zeros (2, 1) ) Array ([[0.], [0.]]) Ones () Creates a full 1 array np.ones based on the specified shape and Dtype (2, 1) Array ([[1.], [ 1.]]) Empty () Creates a null array (random value) based on the specified shape and Dtype np.empty ([2, 2], dtype=int) array ([[-1073741821,-1067949133], [ 496041986, 19249760]])#RandomEye () creates a unit matrix based on the specified edge length and Dtype>>> Np.eye (2, dtype=int) array ([[1, 0], [0,1]]) reshape () decomposition, merging>>> a = Np.array ([[[+]], [4,5,6]]) >>> Np.reshape (A, 6) Array ([1, 2, 3, 4, 5, 6]) >>> Np.reshape (A, (3,-1) Array ([[1, 2], [3, 4], [5, 6]])
Operations between arrays and scalars
a+1a*31//a (divisible) a**0.5
Operations between arrays of the same size
A+ba/ba**b
Index of the array
A[5] a2[2][3] a2[2,3]#The index of the first dimension array, as shown in the above notationslicing a[of arrays5:8] a[:3] = 1#array of slices is assigned a value of 1A2[1:2,: 4]#second Array, index 0 to 3A2[:,:1]#the value of all arrays, indexed to 0a2[:,1]#the value of all arrays, indexed to 1Unlike lists, array slices are not automatically copied, and modifications on the slice array affect the original array. b= A[:4]b[-1] = 250"Workaround: Copy ()"#so B is a new memory address.b = A[:4].copy () b[-1] = 250
Python Quantitative Analysis related modules