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

Working with Matrix in Python

ඔයාගෙ මුළු dataset එකම — rows ඔක්කොම, columns ඔක්කොම — matrix එකක්. ML library ඕනම එකක් ඇතුළතින් pass කරන object එකයි මේක.

Your entire dataset — all rows, all columns — is one matrix. This is the object every ML library passes around internally.

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

Lesson 27 of 64 · Part 2 · Maths & First Models · Maths for ML · bilingual notes (English + සිංහල)

Why this matters. Your entire dataset — all rows, all columns — is one matrix. This is the object every ML library passes around internally.

සිංහලෙන්: ඔයාගෙ මුළු dataset එකම — rows ඔක්කොම, columns ඔක්කොම — matrix එකක්. ML library ඕනම එකක් ඇතුළතින් pass කරන object එකයි මේක.

What you'll use this for. When you write X = df.values before training a model, you're converting a DataFrame into exactly this: a matrix.

සිංහලෙන්: Model එකක් train කරන්නත් කලින් X = df.values ලියනකොට, ඔයා DataFrame එකක් හරියටම මේකට හරවනවා: matrix එකකට.

Key principles

  • Rows are examples, columns are features — always. This one convention (rows = samples, columns = features) is universal across NumPy, Pandas, and scikit-learn — mixing it up is a very common source of shape errors.
    Rows කියන්නෙ examples, columns කියන්නෙ features — හැම වෙලාවෙම

Walkthrough

ExamplePython
import numpy as np

Creating a Matrix using numpy

ExamplePython
matrix_1 = np.array([[2,3],[6,7]])
print(matrix_1)

Output

Output
[[2 3]
 [6 7]]
ExamplePython
matrix_1.shape

Output

Output
(2, 2)
ExamplePython
matrix_2 = np.array([[10,35,45],[50,64,80],[20,15,90]])
print(matrix_2)

Output

Output
[[10 35 45]
 [50 64 80]
 [20 15 90]]
ExamplePython
matrix_2.shape

Output

Output
(3, 3)

Creating Matrices with Random Values

ExamplePython
random_matrix = np.random.rand(3,3)
print(random_matrix)

Output

Output
[[0.83137451 0.34392611 0.73880523]
 [0.45072302 0.19623596 0.34236898]
 [0.36614188 0.93128655 0.15230736]]
ExamplePython
# creating matrix with random integers
random_integer_matrix = np.random.randint(100, size=(4,5))
print(random_integer_matrix)

Output

Output
[[21 68 55  1 38]
 [25 56 35 30 29]
 [69  3 35 59 14]
 [68 53 89 81 49]]

Matrix with all the values as 1

ExamplePython
matrix_3 = np.ones((2,3))
print(matrix_3)

Output

Output
[[1. 1. 1.]
 [1. 1. 1.]]
ExamplePython
matrix_3 = np.ones((2,3), dtype=int)
print(matrix_3)

Output

Output
[[1 1 1]
 [1 1 1]]
ExamplePython
matrix_3 = np.ones((10,10), dtype=int)
print(matrix_3)

Output

Output
[[1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]]

Null Matrix or Zero Matrix

ExamplePython
null_matrix = np.zeros((4,4))
print(null_matrix)

Output

Output
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
ExamplePython
null_matrix = np.zeros((7,7))
print(null_matrix)

Output

Output
[[0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]]

Identity Matrix

ExamplePython
identity_matrix = np.eye(3,3)
print(identity_matrix)

Output

Output
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
ExamplePython
identity_matrix = np.eye(5,5)
print(identity_matrix)

Output

Output
[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]

Transpose of a Matrix

ExamplePython
# matrix with random integer values
a = np.random.randint(100, size=(4,5))
print(a)

Output

Output
[[39 18 53  9 59]
 [49 17 36 52 55]
 [40 43 87 14 40]
 [19 69 75 22 47]]
ExamplePython
transpose_of_a = np.transpose(a)
print(transpose_of_a)

Output

Output
[[39 49 40 19]
 [18 17 43 69]
 [53 36 87 75]
 [ 9 52 14 22]
 [59 55 40 47]]

Keep going: ← Lesson 26 · Lesson 28 → · Track it in the Self Guide