Galle, LK--:--5/6 sites live

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.

10 May 20264 min readSelf Guide · Stage 14#Maths & First Models#Maths for ML#Machine learning

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

ExamplePython
import numpy as np

Matrix Addition

Two Matrices can be added only if they have the same shape

ExamplePython
# creating two matrices

A = np.array([[2,3],[4,5]])

B = np.array([[6,7],[8,9]])
ExamplePython
print(A)

Output

Output
[[2 3]
 [4 5]]
ExamplePython
print(B)

Output

Output
[[6 7]
 [8 9]]
ExamplePython
A.shape

Output

Output
(2, 2)
ExamplePython
B.shape

Output

Output
(2, 2)

Adding two Matrices

ExamplePython
sum = A + B
ExamplePython
print(sum)

Output

Output
[[ 8 10]
 [12 14]]
ExamplePython
# create two matrices with random values
matrix_1 = np.random.randint(10, size=(3,3))
matrix_2 = np.random.randint(20, size=(3,3))
ExamplePython
print(matrix_1)

Output

Output
[[2 7 9]
 [6 0 2]
 [8 3 1]]
ExamplePython
print(matrix_2)

Output

Output
[[18 17 11]
 [ 6 15  2]
 [ 9  8 11]]
ExamplePython
sum = np.add(matrix_1, matrix_2)
ExamplePython
print(sum)

Output

Output
[[20 24 20]
 [12 15  4]
 [17 11 12]]
ExamplePython
# create two matrices with random values
matrix_3 = np.random.randint(10, size=(3,3))
matrix_4 = np.random.randint(20, size=(3,4))
ExamplePython
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

ExamplePython
# creating two matrices

A = np.array([[2,3],[4,5]])

B = np.array([[6,7],[8,9]])
ExamplePython
print(A)

Output

Output
[[2 3]
 [4 5]]
ExamplePython
print(B)

Output

Output
[[6 7]
 [8 9]]
ExamplePython
difference = A - B
ExamplePython
print(difference)

Output

Output
[[-4 -4]
 [-4 -4]]
ExamplePython
# create two matrices with random values
matrix_1 = np.random.randint(10, size=(3,3))
matrix_2 = np.random.randint(20, size=(3,3))
ExamplePython
print(matrix_1)

Output

Output
[[0 7 9]
 [6 9 9]
 [7 9 2]]
ExamplePython
print(matrix_2)

Output

Output
[[ 7  8 16]
 [ 0  9  8]
 [16 17 12]]
ExamplePython
diff = np.subtract(matrix_1, matrix_2)
ExamplePython
print(diff)

Output

Output
[[ -7  -1  -7]
 [  6   0   1]
 [ -9  -8 -10]]

Multiplying a matrix by a scalar

ExamplePython
x = 5
y = np.random.randint(10, size=(4,4))

print(y)

Output

Output
[[0 4 6 7]
 [5 4 6 8]
 [4 3 8 0]
 [8 2 7 8]]
ExamplePython
product = np.multiply(x,y)
ExamplePython
print(product)

Output

Output
[[ 0 20 30 35]
 [25 20 30 40]
 [20 15 40  0]
 [40 10 35 40]]

Multiplying 2 Matrices

ExamplePython
# create two matrices with random values
matrix_3 = np.random.randint(5, size=(3,3))
matrix_4 = np.random.randint(5, size=(3,4))
ExamplePython
print(matrix_3)

Output

Output
[[4 2 2]
 [1 2 3]
 [0 1 4]]
ExamplePython
print(matrix_4)

Output

Output
[[3 1 1 3]
 [2 2 0 3]
 [1 2 3 2]]
ExamplePython
product = np.dot(matrix_3, matrix_4)
ExamplePython
print(product)

Output

Output
[[18 12 10 22]
 [10 11 10 15]
 [ 6 10 12 11]]
ExamplePython
product.shape

Output

Output
(3, 4)
ExamplePython
# create two matrices with random values
matrix_3 = np.random.randint(5, size=(3,3))
matrix_4 = np.random.randint(5, size=(4,4))
ExamplePython
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

ExamplePython
# create two matrices with random values
matrix_3 = np.random.randint(5, size=(3,3))
matrix_4 = np.random.randint(5, size=(3,3))
ExamplePython
print(matrix_3)

Output

Output
[[1 3 1]
 [2 3 3]
 [3 0 2]]
ExamplePython
print(matrix_4)

Output

Output
[[1 0 2]
 [2 2 4]
 [0 0 4]]
ExamplePython
product = np.multiply(matrix_3, matrix_4)
ExamplePython
print(product)

Output

Output
[[ 1  0  2]
 [ 4  6 12]
 [ 0  0  8]]
ExamplePython
# create two matrices with random values
matrix_3 = np.random.randint(5, size=(3,3))
matrix_4 = np.random.randint(5, size=(3,4))
ExamplePython
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