NumPy's ndarray: a multidimensional array object
- ndarray object is an N-dimensional array object and a flexible and fast big data container
You can use the array of ndarray to perform some mathematical operations on the whole block of data - ndarray is a general multi-dimensional container of homogeneous data, that is, the elements in it must be of the same type
- Each array has a shape (tuple representing the size of each dimension) and a dtype (indicating array array type)
Create ndarray
- Using the array function, the array function takes all sequential objects (including other arrays) and produces a new NumPy array with the incoming data
- A nested sequence (such as a long list made up of a set of equal length lists) will be converted into a set of multidimensional arrays
- np.array infers a suitable data type for the new array, which is stored in the dtype object
- np.zero, np.ones and np.empty can create new arrays and return arrays with 0, 1 and no specific value (garbage value). When using these methods to create multidimensional arrays, you need to pass in a tuple representing the shape
- Np.range can return one-dimensional array, which is similar to range
(NumPy focuses on numerical calculation. If there is no special specification, the data type is basically float64.)
import numpy as np # Using the array function to create a new array of ndarray s arr = np.array([1,2,3]) print(arr) # data type print(arr.dtype) print(arr.shape) # asarray converts input to ndarray num = [1,2,3,4,5] arr2 = np.asarray(num) print(arr2) # range function, which returns one-dimensional darray arr3 = np.arange(10) print(arr3) # ones, zeros creates an array of all 1 / all 0 according to the specified shape and dtype arr4 = np.ones((2,2), dtype = 'int16') print(arr4) arr5 = np.zeros((2,3)) print(arr5) # One's like, zero's like create an array similar to the specified array, which is 1 / 0 arr6 = np.ones_like(arr2) print(arr6) arr7 = np.zeros_like((3,4)) print(arr7) # empty creates a new array, allocates only memory space and does not fill any values arr8 = np.empty((2,2)) print(arr8) # Empty? Like creates an array similar to the specified array arr9 = np.empty_like((2,2),dtype='int64') print(arr9) # eye, identity creates a square N*N unit matrix (the diagonal is 1, the rest is 0) arr10 = np.eye((3)) print(arr10) arr11 = np.identity((4),dtype = 'float64') print(arr11)
Data type of darray
- Naming method of dtyped: type name + number representing the bit length of each element
e.g. float64, int16, int32 - Convert dtype through darray's astype method, and a new array (a copy of the original data) will be created
- Note that floating-point numbers, such as float32 and float64, can only represent approximate fractional values. In complex calculations, this operation can only be effective within a certain number of decimal places because some floating-point errors may accumulate
arr = np.array([1,2,3,4,5]) arr.dtype # dtype('int32') float_arr = arr.astype('float64') float_arr.dtype # dtype('float64')
Basic indexes and slices
- When a scalar is assigned to a slice, the value is broadcast to the entire selection. Array slice is the view of the original array. Any modification is made in the source array
- If you want to slice a copy, you need to use arr[1:3].copy()
- High dimensional arrays return indexes in order from low dimension to high dimension
Slice index
1. High dimensional array can pass in multiple slices at a time 2. High dimensional array can mix integer index and slice to return low dimensional slice
# Broadcast of slices arr = np.arange(10) print(arr[1:3]) arr[1:3] = 11 print(arr) # Slice copy arr_copy = arr[:].copy() print(arr_copy) # High dimensional array index arr2d = np.array([[1,2,3],[4,5,6],[7,8,9]]) print(arr_n[0][1]) # Slice high dimensional array arr2d[:2,:1] arr2d[2,:2]
Boolean index
- Boolean index: a Boolean ndarray object
names = np.array(['Bob','Joe','Will','Joe','Joe']) names == 'Joe' # array([False, True, False, True, True])
- You can use a Boolean index as an array index, but the length of the Boolean array must be the same as the length of the axis being indexed
- You can also mix Boolean indexes with slices and integers
Fancy indexing
Index with integer array
Array transpose and axis exchange
- Transpose to return to the source data view. Simple transpose. T
arr = np.arange(15).reshape((3,5)) arr.T
- To transpose a high dimensional array, you need to get a tuple of axis numbers to transpose the axes
arr = np.arange(16).reshape((2,2,4)) arr.transpose((1,0,2))
- Using the swaaxes method, you need to accept a pair of axis numbers and return to the source data view
arr.swapaxes(1,2)