写在前面
关于数据科学环境的建立,可以参考我的博客:
【深耕 Python】Data Science with Python 数据科学(1)环境搭建
往期数据科学博文:
【深耕 Python】Data Science with Python 数据科学(2)jupyter-lab和numpy数组
【深耕 Python】Data Science with Python 数据科学(3)Numpy 常量、函数和线性空间
Question 1
What happens if the dimensions in reshape() don’t match the array size (e.g. np.arange(16).reshape((4, 17)))?
jupyter 代码片段:
import numpy as np
a = np.arange(16).reshape((4, 17))
输出结果:
Question 2
Confirm that A = np.random.rand(5, 5) lets you define a 5 x 5 random matrix.
jupyter 代码片段:
A = np.random.rand(5, 5)
print(A)
A
输出结果:
Question 3
Find the inverse Ainv of the 5 x 5 matrix in the previous exercise. (Calculating the inverse of a 2 x 2 matrix as in Section 11.2.2 is fairly simple by hand, but the task rapidly gets harder as the matrix size increases, in which case a computational system like NumPy is indispensable.)
jupyter 代码片段:
Ainv = np.linalg.inv(A)
print(Ainv)
Ainv
输出结果:
Question 4
What is the matrix product I = A @ Ainv of the matrices in the previous two exercises? Use the same isclose() trick from Listing 11.7 to zero out the elements of I close to zero and confirm that the resulting matrix is indeed the 5 x 5 identity matrix.
jupyter 代码片段:
I = A @ Ainv
print(I)
I[np.isclose(I, 0)] = 0
I
输出结果:
参考文献 Reference
《Learn Enough Python to be Dangerous——Software Development, Flask Web Apps, and Beginning Data Science with Python》, Michael Hartl, Boston, Pearson, 2023.