NumPy the method of creating ndarray is quite sufficient, almost the common method of matrix operation.
Conventions:
Import NumPy as NP
Common functions for creating Ndarray are: Np.array, Np.asarray, Np.arange, Np.ones, Np.ones_like, Np.zeros, Np.zeros_like, Np.empty, np.empty_ Like, Np.eye, np.identity
- Create a nadrray, i.e. Np.array, with an array-like data structure, Np.asarray
Print Np.array ([1, 2, 3], Dtype=np.int)
[1 2 3]
[Finished in 0.1s]
As shown in the code above, the main parameters of the function are Array_like object and dtype= two. Object can be list, nested list, tuple, nested tuple, or ndarray;
Dtype= includes commonly used np.int, np.float and so on.
It should be noted that the array function deeply replicates the data of object and Np.asarray directly references the data of object.
Print Np.array ([[1, 2], [3, 4]], dtype=np.int)
[[1 2]
[3 4]]
[Finished in 0.1s]
The code above generates a 2-D ndarray. In this two-dimensional ndarray, the outermost [] is axis 0 (axis=0), and the inside [] is axis 1. If it is a 3-D Ndarray, the axis 0,1,2 from outside to inside.
Ndarray overloads the [] operation, so its access is similar to the normal Python list. Subsequent index chapters are described in detail.
Np.asarray and Np.array are similar, except that the data is not deeply copied.
Create Naarray (1)