天天看點

NumPy 之 存儲檔案和線性代數

import numpy as np      

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:

arr = np.arange(10)

"儲存數組"
np.save('../examples/some_arry', arr)      
'儲存數組'      

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.

np.load('../examples/some_array.npy')      
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64)      

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.

np.savez("../examples/array_archive.npz", a=arr, b=arr)      
arch = np.load('../examples/array_archive.npz')

'多個數組取出時, 是惰性加載的, 類似生成器'
arch['b']      
'多個數組取出時, 是惰性加載的, 類似生成器'      
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])      

if your data compresses well, you may wish to use numpy.savez_compressed instead:

np.savez_compressed('../examples/arrays_compressed.npz', a=arr, b=arr)      

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.

"2x3"
x = np.array([[1,2,3], [4,5,6]])

"3x2"
y = np.array([[6,23], [-1,7], [8,9]])

x
y      
'2x3'      
'3x2'      
array([[1, 2, 3],
       [4, 5, 6]])      
array([[ 6, 23],
       [-1,  7],
       [ 8,  9]])      
"2x3 * 3x2 = 2x2, 對左邊列向量的線性組合嘛"
x.dot(y)

"x.dot(y) is equivalent to np.dot(x,y)"
np.dot(x,y)      
'2x3 * 3x2 = 2x2, 對左邊列向量的線性組合嘛'      
array([[ 28,  64],
       [ 67, 181]])      
'x.dot(y) is equivalent to np.dot(x,y)'      
array([[ 28,  64],
       [ 67, 181]])      

A matrix product between a two-dimensional array and a suitably sized one-dimensional array results in a one-dimensional array:

np.dot(x, np.ones(3))      
array([ 6., 15.])      

The @ symbol also works as an infix operator(強制插入) that performs matrix multiplication:

x @ np.ones(3)      
array([ 6., 15.])      

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的這些矩陣的函數都是和像這樣的語言用的同一個标準.

from numpy.linalg import inv, qr

x = np.random.randn(5,5)

"計算内積 $A^TA$"
mat = x.T.dot(x)

'矩陣求逆 inv'
inv(mat)      
'計算内積 $A^TA$'      
'矩陣求逆 inv'      
array([[ 2.85462863, -0.08842397, -0.01719878,  0.28840731,  1.33531619],
       [-0.08842397,  0.36385157,  0.05790547,  0.21733807, -0.04179607],
       [-0.01719878,  0.05790547,  0.16845103,  0.03607368, -0.07364311],
       [ 0.28840731,  0.21733807,  0.03607368,  0.33697629,  0.22115989],
       [ 1.33531619, -0.04179607, -0.07364311,  0.22115989,  0.89288396]])      
"矩陣與其逆的積-> 機關陣"
mat.dot(inv(mat))

'矩陣的QR分解, Q是正交矩陣, R是上三角矩陣'
q, r = qr(mat)

q
r      
'矩陣與其逆的積-> 機關陣'      
array([[ 1.00000000e+00, -1.34588695e-17,  1.20337000e-17,
         1.43461087e-16,  1.19991758e-16],
       [-1.44647137e-16,  1.00000000e+00, -2.44723192e-19,
        -3.05504861e-16,  1.85480504e-17],
       [-5.64972079e-16,  1.06290868e-17,  1.00000000e+00,
        -1.24805205e-17, -1.52382375e-17],
       [ 2.64575648e-17, -3.37897541e-18, -3.46206228e-17,
         1.00000000e+00, -2.61606309e-16],
       [-4.05280510e-16, -4.83836278e-17,  2.70272255e-17,
        -3.85392887e-17,  1.00000000e+00]])
      
'矩陣的QR分解, Q是正交矩陣, R是上三角矩陣'
      
array([[-0.49090965, -0.10814586, -0.24518935,  0.10423654,  0.82239231],
       [ 0.00323168, -0.7867822 , -0.00793056, -0.61663426, -0.02574129],
       [ 0.32640742,  0.12703087, -0.92399902, -0.14659492, -0.04535519],
       [-0.1390847 ,  0.59414019,  0.14745407, -0.76639263,  0.13620759],
       [ 0.79568267, -0.01178262,  0.25357916, -0.00701326,  0.54990788]])
      
array([[-2.59814895,  1.59367406,  4.62351953, -3.75681459,  6.16316304],
       [ 0.        , -7.15657133,  0.57430456,  7.77949798, -2.22774585],
       [ 0.        ,  0.        , -6.05158388,  1.46469814, -0.57791469],
       [ 0.        ,  0.        ,  0.        , -2.70965304,  0.66330379],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.61587833]])
      

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