Matrix Introduction
Matrix is a two-dimensional array of numbers often denoted with uppercase letter. Matrix is comprised of rows(m) and columns(n) .
Vector may be considered a matrix with one column and multiple rows.
Create a matrix using NumPy
Copy from numpy import array
# create a matrix
C = array ([[ 'Nepal' , 'Kathmandu' , 29.3 ], [ 'United States' , 'Washington D.C' , 327.16 ]])
print (C)
Copy [['Nepal' 'Kathmandu' '29.3']
['United States' 'Washington D.C' '327.16']]
Matrix Addition
Two matrices with the same dimensions can be added to create a new third matrix. C = A + B
A = [ a 11 a 12 . . a 1 n a 21 a 22 . . a 2 n . . a m 1 a m 2 . . a m n ] , B = [ b 11 b 12 . . b 1 n b 21 b 22 . . b 2 n . . b m 1 b m 2 . . b m n ] C = [ a 11 + b 11 a 12 + b 12 . . a 1 n + b 1 n a 21 + b 21 a 22 + b 22 . . a 2 n + b 2 n . . a m 1 + b m 1 a m 2 + b m 2 . . a m n + b m n ] A=\begin{bmatrix}a_{11} \hspace{0.2cm} a_{12}\hspace{0.2cm} .. \hspace{0.2cm} a_{1n}
\\ a_{21} \hspace{0.2cm} a_{22}\hspace{0.2cm} .. \hspace{0.2cm} a_{2n}
\\ ..
\\ a_{m1} \hspace{0.2cm} a_{m2}\hspace{0.2cm} .. \hspace{0.2cm} a_{mn} \end{bmatrix},
B=\begin{bmatrix}b_{11} \hspace{0.2cm} b_{12}\hspace{0.2cm} .. \hspace{0.2cm} b_{1n}
\\ b_{21} \hspace{0.2cm} b_{22}\hspace{0.2cm} .. \hspace{0.2cm} b_{2n}
\\ ..
\\ b_{m1} \hspace{0.2cm} b_{m2}\hspace{0.2cm} .. \hspace{0.2cm} b_{mn} \end{bmatrix}
\newline
C=\begin{bmatrix}a_{11} + b_{11} \hspace{0.2cm} a_{12} + b_{12}\hspace{0.2cm} .. \hspace{0.2cm} a_{1n} + b_{1n}
\\ a_{21} + b_{21} \hspace{0.2cm} a_{22} + b_{22} \hspace{0.2cm} .. \hspace{0.2cm} a_{2n} + b_{2n}
\\..
\\ a_{m1} + b_{m1} \hspace{0.2cm} a_{m2} + b_{m2}\hspace{0.2cm} .. \hspace{0.2cm} a_{mn} + b_{mn} \end{bmatrix} A = a 11 a 12 .. a 1 n a 21 a 22 .. a 2 n .. a m 1 a m 2 .. a mn , B = b 11 b 12 .. b 1 n b 21 b 22 .. b 2 n .. b m 1 b m 2 .. b mn C = a 11 + b 11 a 12 + b 12 .. a 1 n + b 1 n a 21 + b 21 a 22 + b 22 .. a 2 n + b 2 n .. a m 1 + b m 1 a m 2 + b m 2 .. a mn + b mn
Copy from numpy import array
A = array ([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]])
B = array ([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]])
C = A + B
print (C)
Copy [[ 2 4 6]
[ 8 10 12]]
Matrix-Scalar Multiplication
A matrix can be multiplied by a scalar represented asC = A . b
where b is a scalar. Note: use '*' in Numpy for matrix multiplication
A = [ a 11 a 12 . . a 1 n a 21 a 22 . . a 2 n . . a m 1 a m 2 . . a m n ] , b = s c a l a r C = [ a 11 ∗ b a 12 ∗ b . . a 1 n ∗ b a 21 ∗ b a 22 ∗ b . . a 2 n ∗ b . . a m 1 ∗ b a m 2 ∗ b . . a m n ∗ b ] A=\begin{bmatrix}a_{11} \hspace{0.2cm} a_{12}\hspace{0.2cm} .. \hspace{0.2cm} a_{1n}
\\ a_{21} \hspace{0.2cm} a_{22}\hspace{0.2cm} .. \hspace{0.2cm} a_{2n}
\\ ..
\\ a_{m1} \hspace{0.2cm} a_{m2}\hspace{0.2cm} .. \hspace{0.2cm} a_{mn} \end{bmatrix},
b=scalar
\newline
C=\begin{bmatrix}a_{11} * b \hspace{0.2cm} a_{12} * b\hspace{0.2cm} .. \hspace{0.2cm} a_{1n} * b
\\ a_{21} * b \hspace{0.2cm} a_{22} * b \hspace{0.2cm} .. \hspace{0.2cm} a_{2n} * b
\\..
\\ a_{m1} * b \hspace{0.2cm} a_{m2} * b\hspace{0.2cm} .. \hspace{0.2cm} a_{mn} * b \end{bmatrix} A = a 11 a 12 .. a 1 n a 21 a 22 .. a 2 n .. a m 1 a m 2 .. a mn , b = sc a l a r C = a 11 ∗ b a 12 ∗ b .. a 1 n ∗ b a 21 ∗ b a 22 ∗ b .. a 2 n ∗ b .. a m 1 ∗ b a m 2 ∗ b .. a mn ∗ b
Copy from numpy import array
A = array ([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]])
b = 2
C = A * b
print (C)
Copy [[ 2 4 6]
[ 8 10 12]]
Matrix-Vector Multiplication
A matrix can be multiplied by a vector represented as C = A . v
where v is a vector given it follows the rule of matrix multiplication.
Rule of matrix multiplication
For example, matrix A has the dimensions m rows and n columns and matrix B has the dimensions n and k . The n columns in A and n rows b are equal. The result is a new matrix with m rows and k columns.
C ( m , k ) = A ( m , n ) ∗ B ( n , k ) C(m,k) = A(m,n) * B(n,k)
C ( m , k ) = A ( m , n ) ∗ B ( n , k ) Example of Matrix-Vector multiplication:
A = [ a 11 a 12 a 21 a 22 a 31 a 32 ] , v = [ v 1 v 2 ] C = [ a 11 ∗ v 1 + a 12 ∗ v 2 a 21 ∗ v 1 + a 22 ∗ v 2 a 31 ∗ v 1 + a 32 ∗ v 2 ] A=\begin{bmatrix}
a_{11} \hspace{0.2cm} a_{12}
\\ a_{21} \hspace{0.2cm} a_{22}
\\ a_{31} \hspace{0.2cm} a_{32}
\end{bmatrix},
v=\begin{bmatrix}
v_{1}
\\ v_{2}
\end{bmatrix}
\newline
C=\begin{bmatrix}
a_{11} * v_{1} + a_{12} * v_{2}
\\ a_{21} * v_{1} + a_{22} * v_{2}
\\ a_{31} * v_{1} + a_{32} * v_{2}
\end{bmatrix} A = a 11 a 12 a 21 a 22 a 31 a 32 , v = [ v 1 v 2 ] C = a 11 ∗ v 1 + a 12 ∗ v 2 a 21 ∗ v 1 + a 22 ∗ v 2 a 31 ∗ v 1 + a 32 ∗ v 2
Copy from numpy import array
from numpy import dot
A = array ([[ 1 , 2 ], [ 3 , 4 ], [ 5 , 6 ]])
v = array ([ 0.5 , 0.5 ])
C = dot (A, v)
print (C)
Matrix Multiplication (Hadamard Product)
Two matrices with the same dimension can be simply multiplied element-wise. Also known as Hadamard Product, it is represented with a circle as C = A o B
Note : Implemented using star operator as in Matrix-Scalar Multiplication
A = [ a 11 a 12 a 21 a 22 a 31 a 32 ] , B = [ b 11 b 12 b 21 b 22 b 31 b 32 ] C = [ a 11 ∗ b 11 , a 12 ∗ b 12 a 21 ∗ b 21 , a 22 ∗ b 22 a 31 ∗ b 31 , a 32 ∗ b 32 ] A=\begin{bmatrix}
a_{11} \hspace{0.2cm} a_{12}
\\ a_{21} \hspace{0.2cm} a_{22}
\\ a_{31} \hspace{0.2cm} a_{32}
\end{bmatrix},
B=\begin{bmatrix}
b_{11} \hspace{0.2cm} b_{12}
\\ b_{21} \hspace{0.2cm} b_{22}
\\ b_{31} \hspace{0.2cm} b_{32}
\end{bmatrix}
\newline
C=\begin{bmatrix}
a_{11} * b_{11} , a_{12} * b_{12}
\\ a_{21} * b_{21} , a_{22} * b_{22}
\\ a_{31} * b_{31} , a_{32} * b_{32}
\end{bmatrix} A = a 11 a 12 a 21 a 22 a 31 a 32 , B = b 11 b 12 b 21 b 22 b 31 b 32 C = a 11 ∗ b 11 , a 12 ∗ b 12 a 21 ∗ b 21 , a 22 ∗ b 22 a 31 ∗ b 31 , a 32 ∗ b 32
Copy from numpy import array
A = array ([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]])
B = array ([[ 2 , 2 , 2 ], [ .5 , .5 , .5 ]])
C = A * B
print (C)
Copy [[2. 4. 6. ]
[2. 2.5 3. ]]
Matrix-Matrix Multiplication (Dot Product)
Matrix-Matrix, also called the matrix dot product is a complicated multiplication which must follow the rule of matrix multiplication represented as C = A * B
.
This is made clear with the following image:
A: 4 x 2; B: 2 x 3; C = A * B => 4 x 3
For calculating:
C12(marked with red arrow) = a11*b12 + a12*b22
C33(marked with blue arrow) = a31*b13 + a32*b23
Copy from numpy import array
from numpy import dot
A = array ([[ 1 , 2 ], [ 3 , 4 ], [ 5 , 6 ], [ 7 , 8 ]])
B = array ([[ 2 , 2 , 2 ], [ .5 , .5 , .5 ]])
C = dot (A,B)
print (C)
Copy [[ 3. 3. 3.]
[ 8. 8. 8.]
[13. 13. 13.]
[18. 18. 18.]]
Link:
Introduction to Matrices and Matrix Arithmetic for Machine Learning