17.Numpy
axis=0 列 axis=1 行
# one axis [1, 3, 5] # 3 elements, it has a length of 3. # 2 axes [[ 1, 3, 5], [ 2, 4, 6]] # The first axis has a length of 2, # the second axis has a length of 3.
Create ndarray
import numpy as np # create 1 axis array x = np.arange(3) # [0, 1, 2] print(type(x)) # <class 'numpy.ndarray'> # check if ndarray type isinstance(x, np.ndarray) # True # be explicitly specified type y = np.arange(3, dtype='float64') # [ 0. 1. 2.]
import numpy as np existed_list = [18, 15, 21, 10, 88, 76, 29, 20] np_array = np.array(existed_list) print(np_array) # [18 15 21 10 88 76 29 20]
Attributes of ndarray
import numpy as np x = np.arange(3) # [0, 1, 2] # ndim - the number of axes (dimensions) of the array. print(x.ndim) # 1 dim # shape - the dimensions of the array. print(x.shape) # (3, ) # size - the total number of elements of the array. print(x.size) # 3 # dtype - the type of the elements in the array. print(x.dtype) # int64
Axes reshape
x = np.arange(6) print(x) # [0 1 2 3 4 5] new_shape = x.reshape(2, 3) print(new_shape) # [[0 1 2] # [3 4 5]] # equivalently new_shape = np.reshape(x, (2, 3)) # also can be one line to create and reshpae y = np.arange(6).reshape(2, 3)
Initial placeholder content
# np.zeros - full of zeros np.zeros(3) # array([ 0., 0., 0.]) np.zeros((2, 3)) # array([[ 0., 0., 0.], # [ 0., 0., 0.]]) # np.ones - full of ones np.ones((2,3)) # array([[ 1., 1., 1.], # [ 1., 1., 1.]]) # np.identity - a square array with ones on the main diagonal np.identity(3) # array([[ 1., 0., 0.], # [ 0., 1., 0.], # [ 0., 0., 1.]])
Array Index
import numpy as np # 1-D array x = np.arange(6) # array([0, 1, 2, 3, 4, 5]) x[2] # 2 x[-2] # 4 # 2-D array x = np.arange(6).reshape(2, 3) #[[0, 1, 2], # [3, 4, 5]]) x[0, 2] # 2 x[1, -1] # 5
Array Slice & Stride (1-D array)
import numpy as np x = np.arange(6) # array([0, 1, 2, 3, 4, 5]) x[1:5] # [1, 2, 3, 4] x[:2] # [0, 1] x[1:5:2] # [1, 3]
Array Slice & Stride (2-D array)
import numpy as np x = np.arange(6).reshape(2, 3) #[[0, 1, 2], # [3, 4, 5]]) x[0, 0:2] # [0, 1] x[:, 1:] # [[1, 2], # [4, 5]] x[::1, ::2] # [[0, 2], # [3, 5]]
Boolean / Mask Index
import numpy as np x = np.arange(6) # array([0, 1, 2, 3, 4, 5]) condition = x<3 x[condition] # [0, 1, 2] x[condition] = 0 x # [0, 0, 0, 3, 4, 5]
Concatenate
import numpy as np a = np.array([[1, 2, 3], [4, 5, 6]]) b = np.array([[7, 8, 9]]) np.concatenate((a, b), axis=0) # [[1, 2, 3], # [4, 5, 6], # [7, 8, 9]] c = [[0], [0]] np.concatenate((a, c), axis=1) # [[1, 2, 3, 0], # [4, 5, 6, 0]]
Basic Operations
可以做一個或兩個array的數學運算
import numpy as np a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6], [7, 8]]) print(a + b) # array([[6, 8], [10, 12]]) print(a - b) # array([[-4, -4], [-4, -4]]) print(a * b) # array([[5, 12], [21, 32]]) print(a / b) # array([[0.2, 0.33333333], [0.42857143, 0.5]] print(a - 1) # array([[0, 1], [2, 3]]) print(a * 2) # array([[2, 4], [6, 8]])
Basic Linear Algebra
- 轉置矩陣:m * n 矩陣在向量空間上轉置為 n * m 矩陣
- 逆矩陣:n * n 矩陣 A 存在一個 n * n 矩陣 B,使得 AB = BA = I
import numpy as np a = np.array([[0, 1],[2, 3]]) # 轉置矩陣 print(a.T) #[[0, 2], # [1, 3]] # 逆矩陣 inverse = np.linalg.inv(a) print(inverse) # [[-1.5, 0.5] # [1, 0]] # 內積 print(np.dot(a, inverse)) # [[ 1. 0.] # [ 0. 1.]]
Test
Q1. Create a vector with values ranging from 10 to 49 by np.arange Q2. Reverse a vector (first element becomes last) Q3. Create a 3x3x3 array with random values by np.random.random Q4. Create a 10x10 array with random values and find the minimum and maximum values by np.min, np.max Q5. Add a border (filled with 0's) around an 3 * 3 matrix with 1 by np.pad Q6. Normalize a 5x5 random matrix by divide max number
#Q1 arr=np.arange(10,50) print(arr) #Q2 arr=np.arange(10,50) arr=arr[::-1] print(arr) #Q3 arr=np.random.random((3, 3, 3)) print(arr) #Q4 arr=np.random.random((10,10)) print(arr) np.min(arr) np.max(arr) #Q5 arr=np.ones((3,3)) arr=np.pad(arr,pad_width=1, mode='constant', constant_values=0) print(arr) #Q6 arr=np.random.random((5,5)) print(arr) max=np.max(arr) arr=arr/max print(arr)
Q7. Given a 1D array, negate all elements which are between 3 and 8, in place. Q8. Extract from the array np.array([3,4,6,10,24,89,45,43,46,99,100]) with Boolean masking all the number: (1) which are not divisible by 3 (2) which are divisible by 5 (3) which are divisible by 3 and 5 Q9. Create random vector of size 10 and replace the maximum value by 0 (np.argmax) Q10. Create a 5x5 matrix with row values ranging from 0 to 4
#Q7 arr=np.arange(11) arr[(3<=arr)&(8>=arr)]*=-1 print(arr) #Q8 arr=np.array([3,4,6,10,24,89,45,43,46,99,100]) arr1=arr[arr%3!=0] print(arr1) arr2=arr[arr%5==0] print(arr2) arr3=arr[(arr%3==0)&(arr%5==0)] print(arr3) #Q9 arr=np.random.random(10) print(arr) arr[arr.argmax()]=0 print(arr) #Q10 Z = np.zeros((5, 5)) Z += np.arange(5) print(Z)
數據拼接
- 新增資料
a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6]]) np.concatenate((a, b), axis=0)
array([[1, 2], [3, 4], [5, 6]])
- 新增欄位
a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6]]) np.concatenate((a,b.T),axis = 1)
array([[1, 2, 5], [3, 4, 6]])