Machine Learning From scratch | Part 3. Matrices and matrices Dot Product
Prerequisit : Vector Dot Products
Matrices are grid of numbers or arrangment of numbers . In programming it can be an array of array
a real example can be marks of students (A,B,C) in subjects (S1,S2,S3)
can be shown as Student
A have scored 20 in S1 , 25 in S2 and 30 in S3 = [20,25,30]
B have scored 25 in S1 , 15 in S2 and 20 in S3 = [25,15,20]
C have scored 35 in S1 , 25 in S2 and 30 in S3 = [35,25,30]
S1 S2 S3 ----> Row
A 20 25 30
B 25 15 20
C 35 25 30
|
|
|
V
Columns
Matrix Shape
Every matrix has a shape which is
number of rows * number of columns
for our above matrix we can say it is a 3*3 matrix as it has 3 rows and 3 columns
Matrix Dot Products
we have one matrix A of shape 2*3 (which means we have 2 row and 3 column ) and we have one more matrix B of shape 3*2 (which means we have 3 rows and 2 column )
for matrix multiplications thier is one rule which should be statisfied
A(2*3) B(3*2) the number of columns of first matrix and number of rows in second matrix showld be equal
in First matrix no columns is 3
in Second matrix no rows is 3
the result will be a matrix of shape numer of rows from first matrix * number of columns in second matrix for our example
result = matrix of shape 2*2 (2 rows and 2 columns)
wo we can multiply this two matrices
A = a1 a2 a3 B = b1 b2
a4 a5 a6 b3 b4
b5 b6
AB = a1b1 + a2b3 + a3b5 a1b2 + a2b4 + a3b6
a4b1 + a5b3 + a6b5 a4b2 + a5b4 + a6b6
Lets See by Example
A = [[1,2,3],[4,5,6]]
B = [[1,2],[3,5],[4,2]] --> this has 3 rows and 2 column
A = 1 2 3 B = 1 2
4 5 6 3 5
4 2
AB = 1*1 + 2*3 + 3*4 1*2 + 2*5 + 3*2
4*1 + 5*3 + 6*4 4*2 + 5*5 + 6*2
= 1+6+12 2+10+6
4+15+24 8+25+12
= 19 18
43 45
In the next article we will see how to calculate it in program