Article directory
- foreword
- 1. NumPy basic training
-
- 1.1 Create a one-dimensional ndarray object with a length of 10 and all zeros, and let the fifth element be 1
- 1.2 Create an ndarray object with elements from 10 to 49
- 1.3 Reverse the position of all elements in question 2
- 1.4 Create a 10*10 ndarray object and print the largest and smallest elements
- 1.5 Create a 10*10 ndarray object, and the matrix boundary is all 1, and the inside is all 0
- 1.6 Create a 5*5 matrix with each row ranging from 0 to 4
- 1.7 Create an arithmetic sequence of length 12 ranging from 0-1
- 1.8 Create a random array of length 10 and sort it
- 1.9 Create a random array of length 10 and replace the maximum value with 0
- Two, NumPy intensive training
-
- 2.1 Given a 4-dimensional matrix, find the sum of the last two dimensions
- 2.2 Given an array [1,2,3,4,5], insert 3 0s after each element
- 2.3 Given a two-dimensional matrix, swap two rows of elements
- 2.4 Create a random array with a length of 100,000, use two methods to find the third power, and compare the time taken
- 2.5 Create a 5 * 3 random matrix and a 3 * 2 random matrix, and find the matrix product
- 2.6 The elements of each row of the matrix are subtracted from the average value of the row
- 2.7 Print the following matrix
- 2.8 Regularize a 5*5 random matrix
- epilogue
- Related reading
foreword
Hello everyone! I am the original heart. This issue brings you NumPy case consolidation and strengthening exercises. There are 17 questions in total. Personal test.
1. NumPy basic training
1.1 Create a one-dimensional ndarray object with a length of 10 and all zeros, and let the fifth element be 1
n1 = np.zeros(10,dtype=np.int16)
n1[4] = 1
n1
1.2 Create an ndarray object with elements from 10 to 49
n2 = np.arange(10,50)
n2
1.3 Reverse the position of all elements in question 2
n2[::-1]
1.4 Create a 10*10 ndarray object and print the largest and smallest elements
n4 = np.random.random((10,10))
print(np.max(n4))
print(np.min(n4))
1.5 Create a 10*10 ndarray object, and the matrix boundary is all 1, and the inside is all 0
n5 = np.zeros((10,10),dtype=np.int16)
n5[[0,9]] = 1
n5[:,[0,9]] = 1
print(n5)
1.6 Create a 5*5 matrix with each row ranging from 0 to 4
n6 = np.array(range(0,5))
n6
1.7 Create an arithmetic sequence of length 12 ranging from 0-1
n7 = np.linspace(0,1,num=12)
n7
1.8 Create a random array of length 10 and sort it
n8 = np.random.random(10)
np.sort(n8)
1.9 Create a random array of length 10 and replace the maximum value with 0
n9 = np.random.random(10)
n9[np.argmax(n9)] = 0
print(n9)
Two, NumPy intensive training
2.1 Given a 4-dimensional matrix, find the sum of the last two dimensions
n1 = np.random.randint(1,10,(2,3,4,5)) # 四维数组
display(n1)
np.sum(n1,(2,3))
# axis = 0 表示第一个维度
# axis = 1 表示第二个维度
# axis = 2 表示第三个维度
# axis = 3 表示第四个维度
2.2 Given an array [1,2,3,4,5], insert 3 0s after each element
n = np.arange(1,6)
display(n)
n2 = np.zeros(17,dtype=np.int16)
display(n2)
n2[::4] = n
n2
2.3 Given a two-dimensional matrix, swap two rows of elements
n = np.random.randint(1,10,(3,3))
display(n)
n = n[[1,0,2]] # 交换第一行和第二行
display(n)
2.4 Create a random array with a length of 100,000, use two methods to find the third power, and compare the time taken
n = np.random.randint(0,10,100000)
%timeit n ** 3
%timeit np.power(n,3)
2.5 Create a 5 * 3 random matrix and a 3 * 2 random matrix, and find the matrix product
n1 = np.random.randint(0,10,(5,3))
n2 = np.random.randint(0,10,(3,2))
display(n1,n2)
np.dot(n1,n2)
2.6 The elements of each row of the matrix are subtracted from the average value of the row
n = np.random.randint(0,10,(3,4))
display(n)
# 行平均值
n2 = np.mean(n,axis=1).reshape(3,1)
display(n2)
n - n2
2.7 Print the following matrix
n = np.zeros((8,8),dtype = np.int16)
display(n)
n[::2,1::2] = 1
n[1::2,0::2] = 1
display(n)
2.8 Regularize a 5*5 random matrix
n = np.random.randint(0,10,(5,5))
display(n)
min1 = np.min(n)
max1 = np.max(n)
n = (n - min1) / (max1 - min1)
display(n)
Note: The topic material comes from——"Qianfeng Education"
epilogue
This issue is to share with you these topics! I hope that everyone can do more practical exercises to strengthen and consolidate, so as to better master NumPy.
Related reading
Article direct | Link |
---|---|
Review of last issue | [Data Analysis - Basic Introduction to NumPy⑤] - Basic Operation of NumPy - 2 |
Next issue notice | [Data Analysis - Basic Introduction to Pandas ①] - Pandas Basic Introduction - 1 |