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.
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
import numpy as npCreating a Matrix using numpy
matrix_1 = np.array([[2,3],[6,7]])
print(matrix_1)Output
[[2 3]
[6 7]]matrix_1.shapeOutput
(2, 2)matrix_2 = np.array([[10,35,45],[50,64,80],[20,15,90]])
print(matrix_2)Output
[[10 35 45]
[50 64 80]
[20 15 90]]matrix_2.shapeOutput
(3, 3)Creating Matrices with Random Values
random_matrix = np.random.rand(3,3)
print(random_matrix)Output
[[0.83137451 0.34392611 0.73880523]
[0.45072302 0.19623596 0.34236898]
[0.36614188 0.93128655 0.15230736]]# creating matrix with random integers
random_integer_matrix = np.random.randint(100, size=(4,5))
print(random_integer_matrix)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
matrix_3 = np.ones((2,3))
print(matrix_3)Output
[[1. 1. 1.]
[1. 1. 1.]]matrix_3 = np.ones((2,3), dtype=int)
print(matrix_3)Output
[[1 1 1]
[1 1 1]]matrix_3 = np.ones((10,10), dtype=int)
print(matrix_3)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
null_matrix = np.zeros((4,4))
print(null_matrix)Output
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]null_matrix = np.zeros((7,7))
print(null_matrix)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
identity_matrix = np.eye(3,3)
print(identity_matrix)Output
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]identity_matrix = np.eye(5,5)
print(identity_matrix)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
# matrix with random integer values
a = np.random.randint(100, size=(4,5))
print(a)Output
[[39 18 53 9 59]
[49 17 36 52 55]
[40 43 87 14 40]
[19 69 75 22 47]]transpose_of_a = np.transpose(a)
print(transpose_of_a)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
Finished this one?
Part of Self Guide · Stage 14: ML 2 — Maths & First Models. Progress is saved in this browser.



