# Linear Algebra for Machine Learning and Data Science(Week2: Solving system of linear equations: Elimination) ###### tags: `coursera` `Linear Algebra` `math` [week2](https://www.coursera.org/learn/machine-learning-linear-algebra/home/week/2) ## Machine learning motivation [課程連結](https://www.coursera.org/learn/machine-learning-linear-algebra/lecture/hClHj/machine-learning-motivation) 從一些應用瞭解線性代數能做點什麼。 ### Neural networks - Matrix operations ![](https://hackmd.io/_uploads/Hy_XSWbbh.png) 課程介紹的時候有提過神經網路可以輸入影像,但其實它還可以接受輸入語音。 ### Neural networks - Sound recognition ![](https://hackmd.io/_uploads/SkhwIZ--2.png) 語音的輸入像是聲學監控(acoustic monitoring),這可以應用於追蹤生物或是環境狀態。 ### Neural networks - AI-generated music ![](https://hackmd.io/_uploads/HJG7w---2.png) 這是一個蠻有趣的應用,也就是利用人工智慧來生成音樂。把音樂的資料壓縮成離散編碼(discrete codes),然後利用VAE來訓練生成。 ## Solving non-singular system of linear equations [課程連結](https://www.coursera.org/learn/machine-learning-linear-algebra/lecture/uQ59P/solving-non-singular-system-of-linear-equations) 這節課要來說說如何求解。 ### Solving systems of equations ![](https://hackmd.io/_uploads/B1uvQUMbh.png) 一般來說求解會經過幾個步驟。 ### Manipulating equations ![](https://hackmd.io/_uploads/HJ2B4LzWn.png) 上圖左說明的是,我們可以兩邊同乘某一個常數項,舉例來說一起乘上7,那第一個方程式就會變成$7a+7b = 70$。 上圖右說明的是,我們可以把兩個方程式相加產生一個新的方程式。 ### Systems of equations ![](https://hackmd.io/_uploads/r1TKrIMbn.png) 這邊給出一個範例,首先就讓兩個方程式的$a$的系數通通變成1,這可以利用剛剛說到的兩邊同時做某一個操作的作法來處理。把system 1同除5,把system 2同除4,這樣子這個聯立方程式的$a$的系數就會通通變成1。 接下來再按著剛剛所說的,把兩個方程式做相減,這樣就消除掉$a$,得到$-0.95b=-1.9$,然後再兩邊同乘-0.95,就可以得到$b=2$,那把這個結果帶入隨便一個方程式就可以得到$a=3$。 ### What if one of the coefficients of a is zero? ![](https://hackmd.io/_uploads/Skay8UG-n.png) 這個範例就只是跟你說,如果剛好有一個方程式是沒有$a$的,那就更好解,因為你直接知道$b$是多少,就可以回推$a$了。 ## Solving singular systems of linear equations [課程連結](https://www.coursera.org/learn/machine-learning-linear-algebra/lecture/GA6yy/solving-singular-systems-of-linear-equations) ### What if the system is singular(redunant)? ![](https://hackmd.io/_uploads/BJfBPLf-2.png) 這邊給出的範例是關於聯立方程式為singular的情況。按著剛剛的作法,先讓$a$的系數變成1,然後再把兩個方程式做相減的操作。這時候你會發現,什麼都沒了。因為這是一個冗餘的方程式,所以第二個方程式並沒有辦法給出新的訊息。 理論上最終得到的就是$a+b=10$,然後只要帶入能夠滿足這個等式就行。這也意謂著它是擁有無限多解的。 ### What if the system is singular(contradictory)? ![](https://hackmd.io/_uploads/SyH7OIMWn.png) 這是另一個矛盾的聯立方程式。作法說明跳過,直接給出結果,那就是0永遠不會等於2,這是矛盾的。 課程中提供一個呈現[三維空間](https://www.coursera.org/learn/machine-learning-linear-algebra/ungradedWidget/rMrQB/graphical-representation-of-linear-systems-with-3-variables)的範例,個人覺得對於瞭解三維空間中的求解是有直觀幫助。 ## Solving systems of equations with more variables [課程連結](https://www.coursera.org/learn/machine-learning-linear-algebra/lecture/fXzI7/solving-systems-of-equations-with-more-variables) 課程主要說明如何解三個變數的聯立方程式。 ### Elimination method ![](https://hackmd.io/_uploads/r1qIzo7-h.png) 基本上作法也是類似的步驟,先讓$a$的系數正規化為1,然後拿第一個system去減掉另外兩個system。這一步處理之後就會變成第一個system有$a$,另外兩個system就剩下$b, c$。 ### Elimination method ![](https://hackmd.io/_uploads/HJl0fi7Z2.png) 剩下的就是先解$b, c$,這也是一樣的作法,把$b$先正規化為1,然後再相減,這樣就能夠再一次消除$b$,剩下$c$,得到$c$就能計算$b$,最後就能回頭解出$a$。 ### Elimination method ![](https://hackmd.io/_uploads/rJrlXiXb3.png) 所以最終的結果就是$c=3, b=2, a=4$ ## Matrix row-reduction [課程連結](https://www.coursera.org/learn/machine-learning-linear-algebra/lecture/Rnggo/matrix-row-reduction) Matrix row-reduction也稱為Gaussian elimination。 ### Systems of equation s of matrices ![](https://hackmd.io/_uploads/HkVaNsX-n.png) 剛剛在求解的過程中我們有做一個步驟,把$a$的系數先轉成1,然後把方程式相減去減化變數量來求解。 現在我們要把這個方程式的系數拉出來以矩陣來看,然後讓它變成一個Upper diagonal matrix,也就是對角線下為0的矩陣,這稱為Row echelon form,然後再進一步的讓它變成一個Diagonal matrix,這稱為Reduced row echelon form。 (課程中似乎沒有說明如何做reduced row echelon form,這可以看李宏毅老師的線性代數課程,給出蠻多範例的) ### Systems of equation s of matrices ![](https://hackmd.io/_uploads/ryQfIsXb3.png) 這是一個singualr system,它是冗餘的。 ### Systems of equation s of matrices ![](https://hackmd.io/_uploads/r1MLIjXW2.png) 這也是看過的一個範例。 ### Systems of equation s of matrices ![](https://hackmd.io/_uploads/ry0PIiXbn.png) 這個矩陣在處理過後還是它自己。 ### Row echelon form ![](https://hackmd.io/_uploads/rktVwj7W3.png) 經過row echelon form處理過的矩陣的特性就是: 1. 對角線下方為0 2. 對角線上就是1、0 3. 對角線上為1的相對應的row的值可以是任何值 4. 對角線上為0的相對應的row的值也必需要是0 這種條件下在2x2的矩陣中只會有三種情況: 1. 對角線皆為1 2. 對角線皆為0 3. 對角線一個1一個0 ## Row operations that preserve singularity [課程連結](https://www.coursera.org/learn/machine-learning-linear-algebra/lecture/3UmWw/row-operations-that-preserve-singularity) 我們在聯立方程式中操作的方法也可以用在矩陣上,這稱為row operations,更重要的是,在經過row operation之後,它仍然保留它的奇異性(singularity)。 ### Switching rows ![](https://hackmd.io/_uploads/SJrEOombn.png) 這個範例說明的是,原始的矩陣經過計算其determinant為11,為non-singular matrix,在兩個row做交換(也是一種row operation),它仍然是non-singular matrix。這說明著即使經過row operation,矩陣仍然維持它的奇異性,是singular就是singular,是non-singular就是non-singular。 ### Multiplying a row by a (non-zero) scalar ![](https://hackmd.io/_uploads/Byk1FomWh.png) 這邊範例說明的是,原始的矩陣中將第一個row拿出來乘上10,然後再計算它的determinant,最終結果仍然是非零,矩陣仍然維持是non-singular的狀態。 ### Adding a row to another row ![](https://hackmd.io/_uploads/S1dKKo7-3.png) 這邊範例說明的是,將原始矩陣的兩個row相加之後再來計算它的determinant是不變的,並且仍然是non-singular。