線性代數基礎知識的複習

線性代數基礎知識的複習

機器學習須要一些線性代數的基礎知識。c++

matrix:矩陣

\[ A= \begin{bmatrix} 1402 & 191\\ 1371 & 821\\ 949 & 1437\\ 147&1448\\ \end{bmatrix} \]app

\[ B= \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ \end{bmatrix} \]dom

  • A是一個\(4\times2\)的矩陣,由4行2列組成,而且由兩個中括號括起來。記做\(R^{4\times2}\).
  • B是一個\(2\times3\)的矩陣,由2行3列組成,而且由兩個中括號括起來。記做\(R^{2\times3}\).
  • \(A_{ij}\)用來表示矩陣中的某一個元素,其中\(i\)表明矩陣的行。\(j\)表明矩陣的列
    • \(A_{11}=1402\)
    • \(A_{12}=191\)
    • \(A_{132}=1437\)
    • \(A_{41}=147\)
    • \(A_{43}=undefined\)

vector:向量

\[ y= \begin{bmatrix} 460\\ 232\\ 315\\ 178\\ \end{bmatrix} \]機器學習

  • \(y\)是一組向量,能夠把向量看做是一個\({n\times1}\)的矩陣。此處n=4,因此記做\(R^{4}\)ide

  • \(y_i\)是向量中的第\(i^{th}\)個元素學習

    • \(y_1=460\)
    • \(y_2=232\)
    • \(y_3=315\)
  • 學習太高級語言的朋友必定知道,例如c++中的STL標準庫中vector的index是從0開始算的。而在人們實際生活學習中,大部分人習慣從1開始。所以,在學習機器學習中,咱們通常用1做爲起始,而在編寫程序實現的時候,則切換回0。this

  • 附上一段MATLAB的程序spa

    % The ; denotes we are going back to a new row.
    A = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12]
    
    % Initialize a vector 
    v = [1;2;3] 
    
    % Get the dimension of the matrix A where m = rows and n = columns
    [m,n] = size(A)
    
    % You could also store it this way
    dim_A = size(A)
    
    % Get the dimension of the vector v 
    dim_v = size(v)
    
    % Now let's index into the 2nd row 3rd column of matrix A
    A_23 = A(2,3)
    A =
    
         1     2     3
         4     5     6
         7     8     9
        10    11    12
    
    
    v =
    
         1
         2
         3
    
    
    m =
    
         4
    
    
    n =
    
         3
    
    
    dim_A =
    
         4     3
    
    
    dim_v =
    
         3     1
    
    
    A_23 =
    
         6

矩陣加法

\[ \begin{bmatrix} 1 & 0 \\ 2 & 5 \\ 3 & 1 \\ \end{bmatrix} + \begin{bmatrix} 4 & 0.5 \\ 2 & 5 \\ 0 & 1 \\ \end{bmatrix} = \begin{bmatrix} 5 & 0.5 \\ 4 & 10 \\ 3 & 2 \\ \end{bmatrix} \]scala

  • 上面有一個矩陣加法的例子。code

  • 首先,兩個矩陣維度必須相同,即相同的行數相同的列數。

  • 兩個矩陣加法就是將對位置的數字加起來,而後獲得一個新的矩陣,且這個矩陣和原來兩個矩陣維度相同。

  • 在維度不一樣的狀況下沒法進行加法運算,例如:
    \[ \begin{bmatrix} 1 & 0 \\ 2 & 5 \\ 3 & 1 \\ \end{bmatrix} + \begin{bmatrix} 4 & 0.5 \\ 2 & 5 \\ \end{bmatrix} = \mathop{error} \]

矩陣乘法

\[ 3\times \begin{bmatrix} 1 & 0 \\ 2 & 5 \\ 3 & 1 \\ \end{bmatrix} = \begin{bmatrix} 3 & 0 \\ 6 & 15 \\ 9 & 3 \\ \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 2 & 5 \\ 3 & 1 \\ \end{bmatrix} \times3 \]

  • 上面有一個矩陣乘法的例子,注意是實數乘矩陣。

  • 結果是直接將矩陣的各個元素與實數相乘,獲得一個新的矩陣,維數必定相同

  • 對於實數乘矩陣來講,是先乘仍是後乘不影響結果

  • 除法相似於乘法:
    \[ \begin{bmatrix} 4 & 0 \\ 6 & 3 \\ \end{bmatrix} \setminus 4 = \frac{1}{4} \times \begin{bmatrix} 3 & 0 \\ 6 & 15 \\ \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ \frac{3}{2} & \frac{3}{4} \\ \end{bmatrix} \times3 \]

綜合練習

\[ \begin{eqnarray} & & 3 \times \begin{bmatrix} 1 \\ 4 \\ 2 \\ \end{bmatrix} + \begin{bmatrix} 0\\ 0 \\ 5 \\ \end{bmatrix} - \begin{bmatrix} 3 \\ 0 \\ 2 \\ \end{bmatrix} \setminus 3 \\ & = & \begin{bmatrix} 3 \\ 12 \\ 6 \\ \end{bmatrix} + \begin{bmatrix} 0 \\ 0 \\ 5 \\ \end{bmatrix} - \begin{bmatrix} 1 \\ 0 \\ \frac{2}{3} \\ \end{bmatrix}\\ & = & \begin{bmatrix} 2 \\ 12 \\ \frac{31}{3} \\ \end{bmatrix}\\ \end{eqnarray} \]

  • MATLAB代碼:

    % Initialize matrix A and B 
    A = [1, 2, 4; 5, 3, 2]
    B = [1, 3, 4; 1, 1, 1]
    
    % Initialize constant s 
    s = 2
    
    % See how element-wise addition works
    add_AB = A + B 
    
    % See how element-wise subtraction works
    sub_AB = A - B
    
    % See how scalar multiplication works
    mult_As = A * s
    
    % Divide A by s
    div_As = A / s
    
    % What happens if we have a Matrix + scalar?
    add_As = A + s
    A =
    
         1     2     4
         5     3     2
    
    
    B =
    
         1     3     4
         1     1     1
    
    
    s =
    
         2
    
    
    add_AB =
    
         2     5     8
         6     4     3
    
    
    sub_AB =
    
         0    -1     0
         4     2     1
    
    
    mult_As =
    
         2     4     8
        10     6     4
    
    
    div_As =
    
        0.5000    1.0000    2.0000
        2.5000    1.5000    1.0000
    
    
    add_As =
    
         3     4     6
         7     5     4

矩陣與向量相乘

\[ \begin{bmatrix} 1 & 3 \\ 4 & 0 \\ 2 & 1 \\\end{bmatrix} \times\begin{bmatrix} 1 \\ 5 \\\end{bmatrix} =\begin{bmatrix} 16^{(1)} \\ 4^{(2)} \\ 7^{(3)} \\\end{bmatrix} \\\begin{eqnarray} 1 \times 1 + 3 \times 5 = 16 \tag{1}\\ 4 \times 1 + 0 \times 5 = 4 \tag{2}\\ 2 \times 1 + 1 \times 5 = 7 \tag{3}\\\end{eqnarray} \]

  • 上面有一個特殊例子,展現了矩陣與向量相乘的等式和過程

  • 相乘的條件:

    • 設矩陣爲\(A\),向量爲\(B\)
    • \(A_j=B_i\)(A的列數等於B的行數)
  • 將A的一行和B的一列的每一個元素相乘,並相加獲得一個數值。

  • 新的獲得的矩陣的行數與矩陣相同,列數與向量相同。

    能夠參考一下下面這個例子:
    \[ \begin {bmatrix} a & b \\ c & d \\ e & f \\\end {bmatrix} *\begin {bmatrix} x \\ y \\\end {bmatrix} =\begin {bmatrix} a*x + b*y \\ c*x + d*y \\ e*x + f*y \\\end {bmatrix} \]

  • MATLAB代碼:

    % Initialize matrix A 
    A = [1, 2, 3; 4, 5, 6;7, 8, 9] 
    
    % Initialize vector v 
    v = [1; 1; 1] 
    
    % Multiply A * v
    Av = A * v
    A =
    
         1     2     3
         4     5     6
         7     8     9
    
    
    v =
    
         1
         1
         1
    
    
    Av =
    
         6
        15
        24

矩陣與矩陣相乘

咱們如今開始計算這樣一個算式
\[ \begin {bmatrix} 1 & 3 & 2 \\ 4 & 0 & 1 \\\end {bmatrix} *\begin {bmatrix} 1 & 3 \\ 0 & 1 \\ 5 & 2 \\\end {bmatrix} \]
用剛剛學過的矩陣乘向量,將第二個矩陣拆成兩個向量
\[ \begin {bmatrix} 1 & 3 & 2 \\ 4 & 0 & 1 \\\end {bmatrix} *\begin {bmatrix} 1 \\ 0 \\ 5 \\\end {bmatrix} =\begin {bmatrix} 11 \\ 9 \\\end {bmatrix} \]

\[ \begin {bmatrix} 1 & 3 & 2 \\ 4 & 0 & 1 \\\end {bmatrix} *\begin {bmatrix} 3 \\ 1 \\ 2 \\\end {bmatrix} =\begin {bmatrix} 10 \\ 14 \\\end {bmatrix} \]

其實咱們已經計算完成了,只差最後一步,按原來列的順序將答案合併,能夠獲得
\[ \begin {bmatrix} 1 & 3 & 2 \\ 4 & 0 & 1 \\\end {bmatrix} *\begin {bmatrix} 1 & 3 \\ 0 & 1 \\ 5 & 2 \\\end {bmatrix} =\begin {bmatrix} 11 & 10 \\ 9 & 14 \\\end {bmatrix} \]

  • 相乘的條件:

    • 設矩陣1爲\(A\),矩陣2爲\(B\)
    • \(A_j=B_i\)(A的列數等於B的行數)
  • 將A的一行和B的一列的每一個元素相乘,並相加獲得一個數值。

  • 新的獲得的矩陣的行數與A相同,列數與B相同。即\(R^{m*n} \times R^{n*o} = R^{m*o}\)

    能夠參考一下下面這個例子:
    \[ \begin {bmatrix} a & b \\ c & d \\ e & f \\ \end {bmatrix} * \begin {bmatrix} w & x \\ y & z \\ \end {bmatrix} = \begin {bmatrix} a*w + b*y & a*x + b*z\\ c*w + d*y & c*x + d*z\\ e*w + f*y & e*x + f*z\\ \end {bmatrix} \]

  • MATLAB代碼:

    % Initialize a 3 by 2 matrix 
    A = [1, 2; 3, 4;5, 6]
    
    % Initialize a 2 by 1 matrix 
    B = [1; 2] 
    
    % We expect a resulting matrix of (3 by 2)*(2 by 1) = (3 by 1) 
    mult_AB = A*B
    
    % Make sure you understand why we got that result
    A =
    
       1   2
       3   4
       5   6
    
    B =
    
       1
       2
    
    mult_AB =
    
        5
       11
       17

矩陣乘法的一些性質

  1. 不可交換(in general)

    在實數乘法中,兩個數交換以後結果相同是一個常識:
    \[ 3+5=5+3 \]
    這是再正常不過的了。不過矩陣也是如此嗎?

    咱們用上面的矩陣乘法嘗試一下:
    \[ \begin {bmatrix} 1 & 1 \\ 0 & 0 \\ \end {bmatrix} * \begin {bmatrix} 0 & 0 \\ 2 & 0 \\ \end {bmatrix} = \begin {bmatrix} 2 & 0 \\ 0 & 0 \\ \end {bmatrix} \]

    \[ \begin {bmatrix} 0 & 0 \\ 2 & 0 \\ \end {bmatrix} * \begin {bmatrix} 1 & 1 \\ 0 & 0 \\ \end {bmatrix} = \begin {bmatrix} 0 & 0 \\ 2 & 2 \\ \end {bmatrix} \]

    看到了嗎,結果是不同的。

    可是這是通常狀況,有一種狀況,是能夠交換的。

  2. 可交換的特殊狀況(Identity matrix)

    有一種矩陣咱們叫作單位矩陣(Identity matrix),其特色是:

    • 矩陣必定是\(n \times n\)的,記做$I \space or \space I_{n \times n} $

    • 矩陣對角線必定是1,其餘部分必定是0
      \[ \mathop{ \begin {bmatrix} 1 & 0 \\ 0 & 1 \\ \end {bmatrix} }\limits_{2 \times 2} \space \space \space \space \space \space \space \space \space \space \mathop{ \begin {bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ \end {bmatrix} }\limits_{3 \times 3} \space \space \space \space \space \space \space \space \space \space \mathop{ \begin {bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ \end {bmatrix} }\limits_{4 \times 4} \space \space \space \space \space \space \space \space \space \space \mathop{ \begin {bmatrix} 1 & & & & & \\ & 1 & & & & \\ & & 1 & & & \\ & & & 1 & & \\ & & & & \ddots & \\ & & & & & 1 \\ \end {bmatrix} }\limits_{n \times n} \]

    與單位矩陣相乘,單位矩陣不論怎麼交換,結果都不會變。

    • MATLAB代碼:

      % Initialize random matrices A and B 
      A = [1,2;4,5]
      B = [1,1;0,2]
      
      % Initialize a 2 by 2 identity matrix
      I = eye(2)
      
      % The above notation is the same as I = [1,0;0,1]
      
      % What happens when we multiply I*A ? 
      IA = I*A 
      
      % How about A*I ? 
      AI = A*I 
      
      % Compute A*B 
      AB = A*B 
      
      % Is it equal to B*A? 
      BA = B*A 
      
      % Note that IA = AI but AB != BA
      A =
      
         1   2
         4   5
      
      B =
      
         1   1
         0   2
      
      I =
      
      Diagonal Matrix
      
         1   0
         0   1
      
      IA =
      
         1   2
         4   5
      
      AI =
      
         1   2
         4   5
      
      AB =
      
          1    5
          4   14
      
      BA =
      
          5    7
          8   10

矩陣的倒數(逆矩陣)

倒數的概念很熟悉吧。一個數和另外一個數相乘等與1咱們就認爲這對數字互爲倒數。
\[ 3 \times (3^{-1}) = 1 \\ 5 \times (5^{-1}) = 1 \\ \]
對於矩陣,咱們也有一樣的概念。因爲咱們認爲單位矩陣和實數中1的地位相同,所以它是這樣表述的:
\[ A(A^{-1})=(A^{-1})A=I \]
咱們稱\(A^{-1}\)爲逆矩陣。
\[ \mathop{ \begin {bmatrix} 3 & 4 \\ 2 & 16 \\ \end {bmatrix} }\limits_A \mathop{ \begin {bmatrix} 0.4 & -0.1 \\ -0.05 & 0.075 \\ \end {bmatrix} }\limits_{A^{-1}} = \mathop{ \begin {bmatrix} 1 & 0 \\ 0 & 1 \\ \end {bmatrix} }\limits_{AA^{-1}} = I_{2 \times 2} \]
一些要注意的點:

  • 存在逆矩陣的矩陣必定是方陣
  • \(\begin {bmatrix} 0 & 0 \\ 0 & 0 \\ \end {bmatrix}\)像這樣的0矩陣是沒有的逆矩陣的,由於不管如何都沒法讓它變成單位矩陣。你能夠將沒有逆矩陣的方陣近似成零矩陣看。
  • 沒有逆矩陣的矩陣咱們稱之爲奇異矩陣或者是退化矩陣

矩陣的倒置

咱們如今有一個矩陣:
\[ A= \begin {bmatrix} 1 & 2 & 0 \\ 3 & 5 & 9 \\ \end {bmatrix} \]
而它的倒置矩陣就是:
\[ A^T = \begin {bmatrix} 1 & 3 \\ 2 & 5 \\ 0 & 9 \\ \end {bmatrix} \]

  • 這個操做能夠當作是,把A的每個行向量改爲值相同的列向量,再按順序拼接起來。

  • \(A\)通過轉置以後,\(A\)\(A^T\)中每一個元素的對應關係是
    \[ A^T_{ij}=A_{ji} \]

  • MATLAB代碼

    % Initialize matrix A 
    A = [1,2,0;0,5,6;7,0,9]
    
    % Transpose A 
    A_trans = A' 
    
    % Take the inverse of A 
    A_inv = inv(A)
    
    % What is A^(-1)*A? 
    A_invA = inv(A)*A
    A =
    
       1   2   0
       0   5   6
       7   0   9
    
    A_trans =
    
       1   0   7
       2   5   0
       0   6   9
    
    A_inv =
    
       0.348837  -0.139535   0.093023
       0.325581   0.069767  -0.046512
      -0.271318   0.108527   0.038760
    
    A_invA =
    
       1.00000  -0.00000   0.00000
       0.00000   1.00000  -0.00000
      -0.00000   0.00000   1.00000
相關文章
相關標籤/搜索