Matrix Operations in Python
Matrix multiplication කියන්නෙ, ඇත්තටම, Linear Regression එකේ prediction එකක් සහ neural network එකක layer එකක්, ගණනය කරන එකම operation එක.
Matrix multiplication is, quite literally, the single operation that computes every prediction in Linear Regression and every layer of a neural network.
Lesson 28 of 64 · Part 2 · Maths & First Models · Maths for ML · bilingual notes (English + සිංහල)
Why this matters. Matrix multiplication is, quite literally, the single operation that computes every prediction in Linear Regression and every layer of a neural network.
සිංහලෙන්: Matrix multiplication කියන්නෙ, ඇත්තටම, Linear Regression එකේ prediction එකක් සහ neural network එකක layer එකක්, ගණනය කරන එකම operation එක.
What you'll use this for. You won't do this maths by hand once you start using scikit-learn — but understanding it is what lets you debug a model that isn't learning.
සිංහලෙන්: scikit-learn පාවිච්චි කරන්න පටන් ගත්තට පස්සෙ මේ ගණිතය අතින් කරන්නෙ නෑ — ඒත් ඒක තේරුම් ගැනීමයි, ඉගෙන ගන්නෙ නැති model එකක් debug කරන්න ඔයාට ඉඩ දෙන්නෙ.
Key principles
- Order matters: A × B is not the same as B × A. Unlike ordinary numbers, matrix multiplication is not commutative — this single fact explains a huge chunk of "shape mismatch" errors in deep learning code.
Order වැදගත්: A × B කියන්නෙ B × A වගේ එකක් නෙවෙයි
Walkthrough
Importing Numpy Library
import numpy as npMatrix Addition
Two Matrices can be added only if they have the same shape
# creating two matrices
A = np.array([[2,3],[4,5]])
B = np.array([[6,7],[8,9]])print(A)Output
[[2 3]
[4 5]]print(B)Output
[[6 7]
[8 9]]A.shapeOutput
(2, 2)B.shapeOutput
(2, 2)Adding two Matrices
sum = A + Bprint(sum)Output
[[ 8 10]
[12 14]]# create two matrices with random values
matrix_1 = np.random.randint(10, size=(3,3))
matrix_2 = np.random.randint(20, size=(3,3))print(matrix_1)Output
[[2 7 9]
[6 0 2]
[8 3 1]]print(matrix_2)Output
[[18 17 11]
[ 6 15 2]
[ 9 8 11]]sum = np.add(matrix_1, matrix_2)print(sum)Output
[[20 24 20]
[12 15 4]
[17 11 12]]# create two matrices with random values
matrix_3 = np.random.randint(10, size=(3,3))
matrix_4 = np.random.randint(20, size=(3,4))sum_2 = np.add(matrix_3, matrix_4)The notebook raised: ValueError: operands could not be broadcast together with shapes (3,3) (3,4)
We get this error because shape of matrix_3 and matrix_4 are different
Matrix Subtraction
# creating two matrices
A = np.array([[2,3],[4,5]])
B = np.array([[6,7],[8,9]])print(A)Output
[[2 3]
[4 5]]print(B)Output
[[6 7]
[8 9]]difference = A - Bprint(difference)Output
[[-4 -4]
[-4 -4]]# create two matrices with random values
matrix_1 = np.random.randint(10, size=(3,3))
matrix_2 = np.random.randint(20, size=(3,3))print(matrix_1)Output
[[0 7 9]
[6 9 9]
[7 9 2]]print(matrix_2)Output
[[ 7 8 16]
[ 0 9 8]
[16 17 12]]diff = np.subtract(matrix_1, matrix_2)print(diff)Output
[[ -7 -1 -7]
[ 6 0 1]
[ -9 -8 -10]]Multiplying a matrix by a scalar
x = 5
y = np.random.randint(10, size=(4,4))
print(y)Output
[[0 4 6 7]
[5 4 6 8]
[4 3 8 0]
[8 2 7 8]]product = np.multiply(x,y)print(product)Output
[[ 0 20 30 35]
[25 20 30 40]
[20 15 40 0]
[40 10 35 40]]Multiplying 2 Matrices
# create two matrices with random values
matrix_3 = np.random.randint(5, size=(3,3))
matrix_4 = np.random.randint(5, size=(3,4))print(matrix_3)Output
[[4 2 2]
[1 2 3]
[0 1 4]]print(matrix_4)Output
[[3 1 1 3]
[2 2 0 3]
[1 2 3 2]]product = np.dot(matrix_3, matrix_4)print(product)Output
[[18 12 10 22]
[10 11 10 15]
[ 6 10 12 11]]product.shapeOutput
(3, 4)# create two matrices with random values
matrix_3 = np.random.randint(5, size=(3,3))
matrix_4 = np.random.randint(5, size=(4,4))product = np.dot(matrix_3, matrix_4)The notebook raised: ValueError: shapes (3,3) and (4,4) not aligned: 3 (dim 1) != 4 (dim 0)
Element wise Matrix Multiplication
# create two matrices with random values
matrix_3 = np.random.randint(5, size=(3,3))
matrix_4 = np.random.randint(5, size=(3,3))print(matrix_3)Output
[[1 3 1]
[2 3 3]
[3 0 2]]print(matrix_4)Output
[[1 0 2]
[2 2 4]
[0 0 4]]product = np.multiply(matrix_3, matrix_4)print(product)Output
[[ 1 0 2]
[ 4 6 12]
[ 0 0 8]]# create two matrices with random values
matrix_3 = np.random.randint(5, size=(3,3))
matrix_4 = np.random.randint(5, size=(3,4))product = np.multiply(matrix_3, matrix_4)The notebook raised: ValueError: operands could not be broadcast together with shapes (3,3) (3,4)
Keep going: ← Lesson 27 · Lesson 29 → · Track it in the Self Guide
Finished this one?
Part of Self Guide · Stage 14: ML 2 — Maths & First Models. Progress is saved in this browser.



