File Input and Output
NumPy is able to save and load data to and from disk either in text or binary format. In this section I only discuss NumPy's built-in binary format, since most users wil prefer pandas and other tools for loading text or tabular data.
np.save and np.load are the two workhorse functions(主要的函数) for efficiently saving and loading array data on disk. Arrays are saved by default in an uncompressed(未压缩的) raw binary format with file extension .npy:
If the file path does not already end in .npy, the extension will be appended. The array o disk can then be oaded with np.load.
You save multiple arrays in an uncompressed(未解压的) archive using np.savez and passing the arrays as keyword arguments:
When loading an .npz file, you get back a dict-like object that loads the individual(个别的) arrays lazily.
if your data compresses well, you may wish to use numpy.savez_compressed instead:
Linear Algebra
Linear algera, like matrix multiplication(乘法), decompositions(分解), determinats(秩), and other square matrix math, is an important part of any array library. Unlike some languages lile MATLIB, multiplying two, two-dimensional array with * is an element-wise product instead of a matrix dot product.(python 中的星号, 表示两个数组的对应元素乘, 而非矩阵乘哦) Thus, there is a function dot, both an array method and a function in the numpy namespace, for matrix multiplication.
A matrix product between a two-dimensional array and a suitably sized one-dimensional array results in a one-dimensional array:
The @ symbol also works as an infix operator(强制插入) that performs matrix multiplication:
numpy.linalg has a stardard set of matrix decompositions and things like inverse and determinant. These are implemented(被运行) under the hood via(通过) the same industry-standard linear algebra libraies used in other languages like MATLAB and R.. -> Python的这些矩阵的函数都是和像这样的语言用的同一个标准.
The express x.T.dot(x) computes the dot product of x with its transpose x.T
See Table 4-7 for a list of some of the most commonly used linear algbrea functions.
- diag 对角化
- dot 矩阵乘法
- trace 迹: Compute the sum of the diagonal elements
- det 行列式 Compute the matrix determinant
- inv 逆 Compute the inverse of a square matrix
- eig, qr, svd 矩阵的谱分解, QR分解, SVD 分解
- solve 线性方程的解 Solve the linear system Ax=b for x where A is a square matrix
- lstsq 最小二乘近似解 Compute the least-squares solution to Ax=b
耐心和恒心, 总会获得回报的.