본문 바로가기

BITS

[Machine Learning by Stanford] Linear Algebra Review - Matrix Matrix Multiplication

This is a brief summary of ML course provided by Andrew Ng and Stanford in Coursera.

You can find the lecture video and additional materials in 

https://www.coursera.org/learn/machine-learning/home/welcome

 

Coursera | Online Courses From Top Universities. Join for Free

1000+ courses from schools like Stanford and Yale - no application required. Build career skills in data science, computer science, business, and more.

www.coursera.org

To solve for the params $\theta_0$ and $\theta_1$ all in one shot, without needing an iterative algorithm like gradient descent.

Matrix-multiplication is one of the key steps that you need to know. 

 

$\left[\begin{array} {rrr} 1 & 3 & 2 \\ 4 & 0 & 1 \end{array}\right] $ $\left[\begin{array} {rrr} 1 & 3 \\ 0 & 1 \\ 5 & 2 \end{array}\right] $ = $\left[\begin{array} {rrr} 11 & 10 \\ 9 & 14 \end{array}\right] $

 

$\left[\begin{array} {rrr} 1 & 3 & 2 \\ 4 & 0 & 1 \end{array}\right] $ $\left[\begin{array} {rrr} 1 \\ 0  \\ 5  \end{array}\right] $ = $\left[\begin{array} {rrr} 11  \\ 9  \end{array}\right] $

$\left[\begin{array} {rrr} 1 & 3 & 2 \\ 4 & 0 & 1 \end{array}\right] $ $\left[\begin{array} {rrr} 3 \\ 1  \\ 2  \end{array}\right] $ = $\left[\begin{array} {rrr} 10  \\ 14  \end{array}\right] $

Take this two vectors' elements and plugging them together. 

 

Works only if m x n , n  x o (where n matches) to output m x o matrix. 

Example: 

$\left[\begin{array} {rrr} 1 & 3 \\ 2 & 5 \end{array}\right] $ $\left[\begin{array} {rrr}0  & 1 \\ 3 & 2  \end{array}\right] $ = $\left[\begin{array} {rrr} 9 & 7  \\ 15 & 12 \end{array}\right] $

$\left[\begin{array} {rrr} 1 & 3 \\ 2 & 5 \end{array}\right] $ $\left[\begin{array} {rrr}0  \\ 3  \end{array}\right] $ = $\left[\begin{array} {rrr} 9  \\ 15  \end{array}\right] $

$\left[\begin{array} {rrr} 1 & 3 \\ 2 & 5 \end{array}\right] $ $\left[\begin{array} {rrr}1  \\ 2  \end{array}\right] $ = $\left[\begin{array} {rrr} 7  \\ 12  \end{array}\right] $

 

Quiz: In the equation $\left[\begin{array} {rrr} 1 & 3 \\ 2 & 4 \\ 0 & 5 \end{array}\right] $ $\left[\begin{array} {rrr} 1  & 0 \\ 2 & 3  \end{array}\right] $ = $\left[\begin{array} {rrr} 7 & 9  \\ a & b \\ c & d \end{array}\right] $ what is a,b,c,d?

 

Answer: 10, 12, 10, 15

 

Lecturer's Note

We multiply two matrices by breaking it into several vector multiplications and concatenating the result.

 

$\left[\begin{array} {rrr} a & b \\ c & d \\ e & f \end{array}\right] $ $\left[\begin{array} {rrr}w & x  \\ y & z  \end{array}\right] $ = $\left[\begin{array} {rrr} aw+by & ax + bz \\ cw + dy & cx + dz \\ ew + fy & ex + fz  \end{array}\right] $

 

An m x n matrix multiplied by an n x o matrix results in an m x o matrix. In the above example, a 3 x 2 matrix times a 2 x 2 matrix resulted in a 3 x 2 matrix.

To multiply two matrices, the number of columns of the first matrix must equal the number of rows of the second matrix.