Ndarray: Multidimensional Array objects
Ndarray is a generic homogeneous data multidimensional container in which each array has a shape (representing the dimension size) and Dtype (an object that describes the array data type):
Eg:>>>data.shape
(2,3)
>>>data.dtype
Dtype (' float64 ')
1. Create Ndarray
Data1 creating an Ndarray array of arr1
Data1 = [1.2,23,3,23,4,6= Np.array (data1)
Zeros (10,10) and ones (10,10) can create an array of lengths and dimensions, and empty can create a null array
NumPy concern is the numerical calculation, therefore, if does not have the general formulation, the data type basically is float64.
Np.arange (#返回一个ndarray而不是列表)
The Ndarray data type Dtype is a special object that contains the information that Ndarray needs to interpret a piece of memory as a specific data type:
arr1 = Np.array ([1,2,3],dtype=np.float64)
Ndarray can explicitly convert its dtype by means of Astype:
Float_arr = Arr.astype (Np.float64)
An array of equal size can be used to operate the operation to the element level. Operations between arrays of different sizes are called broadcasts.
2. Basic indexes and slices
Ah ah ah ah, daddy's browser, my notes are all lost, and then fill it up!
3. Boolean index
>>>ImportNumPy as NP>>> names = Np.array (['Bob','Joe',' would','Bob',' would','Joe','Joe'])>>> data = Np.random.randn (7,4)>>> names = ='Bob' #generating an array of Boolean typesArray ([True, False, False, True, False, False, false], Dtype=bool)
>>> Data[names = = ' Bob ']
Array ([[0.91085438,-0.83674359, 1.2117743,-0.40052236],
[0.2068526, -0.41068779, 0.83953301, -0.93918484]])
You can also combine slices:
>>> Data[names = ='Bob',: 2]array ([[0.91085438,-0.83674359], [ 0.2068526,-0.41068779]])>>> Data[data<0] = 0#assign values that are less than 0>>>DataArray ([[0.91085438, 0. , 1.2117743, 0. ], [0. , 1.08886269, 1.82398061, 2.28503012], [0. , 1.33202507, 0. , 0. ], [ 0.2068526, 0. , 0.83953301, 0. ], [0. , 0.13073222, 0.33671297, 0. ], [0. , 0.62412247, 0. , 0. ], [ 0.68182239, 0. , 0. , 0. ]])
4. Fancy Index
Refers to an array of integers for indexing, assuming an existing 8*4 array:
Arr = Np.arange (+). Reshape ((8,4))>>> arrarray ([[0, 1, 2, 3], 4, 5, 6, 7], 8, 9, ten, one], [a], [+], [ (+), [28, 29, 30, 31, [+], [+], [+]. ]])>>> arr[[1,5,7,2],[0,3,1,24, 23, 29, 10])
Get a rectangular slice of an array
>>> arr[[1,5,7,2]][:,[0,3,1,24, 7, 5, 6], [all, Max, +], [ (8, 9, 10])), and
You can also use this code to get the results above
>>> arr[np.ix_ ([1,5,7,2],[0,3,1,24, 7, 5, 6], [20, 23, 21, 22 ], [[ 8], 9, 10]])
5. Array Transpose and Axis swapping
T attribute:
arr = Np.arange. Reshape ((3,5))>>> arrarray ([[0, 1, 2, 3, 4], 5, 6, 7, 8, 9], [Ten, one, one, and up]])>>> arr. Tarray ([[0, 5, ten ], 1, 6, one], 2, 7, [], 3, 8] , 4, 9, 14]])
Using NP.DOT to compute the inner product of matrices:
>>> arr = np.random.randn (6,3)>>>3.67517253, -0.57586473, -3.36499059], [-0.57586473, 9.52179993, -0.74028303], [-3.36499059, -0.74028303, 3.42469162]]
A high-dimensional array needs to have a ganso that consists of an axis number to transpose:
Arr = Np.arange (+). Reshape ((2,2,4))>>> Arrarray ([[[0, 1, 2, 3], 4, 5, 6, 7]], 8, 9, ten, one], [one, one, Ten]]]) >>> Arr.transpose ((1,0,2)) array ([[[[[ 0], 1, 2, 3], 8, 9, 10 , all]], 4, 5, 6, 7], [12, 13, 14, 15]]
Swapaxes Method:
>>> arr.swapaxes Array ([[[[0, 4], 1, 5], 2, 6], 3, 7]], 8, [], 9, +], [+], [ 11, 15]])
swapaxes is also a view that returns the source data
Data analysis using Python-02