Complete Numpy Tutorial in Python
Model එකක් touch කරන හැම number එකක්ම — features, weights, predictions — යටින් store වෙන්නෙ NumPy array එකක් විදිහට. Pandas, scikit-learn, TensorFlow — මේ ඔක්කොම හදලා තියෙන්නෙ NumPy උඩ.
Every number a model touches — features, weights, predictions — is stored as a NumPy array under the hood. Pandas, scikit-learn, and TensorFlow are all built on top of it.
Lesson 9 of 64 · Part 1 · Python & Data Basics · Python Libraries · bilingual notes (English + සිංහල)
Why this matters. Every number a model touches — features, weights, predictions — is stored as a NumPy array under the hood. Pandas, scikit-learn, and TensorFlow are all built on top of it.
සිංහලෙන්: Model එකක් touch කරන හැම number එකක්ම — features, weights, predictions — යටින් store වෙන්නෙ NumPy array එකක් විදිහට. Pandas, scikit-learn, TensorFlow — මේ ඔක්කොම හදලා තියෙන්නෙ NumPy උඩ.
What you'll use this for. Learn this once, and Pandas (next lesson) and every ML algorithm after it will make far more sense, because they all speak this same language of arrays.
සිංහලෙන්: මේක එකපාරක් ඉගෙන ගත්තම, ඊළඟ පාඩම වන Pandas සහ ඊට පස්සෙ එන හැම ML algorithm එකක්ම වඩා තේරෙනවා — ඒ ඔක්කොම කතා කරන්නෙ arrays කියන එකම භාෂාවෙන්.
Key principles
- Arrays are fast because they're one type, one block of memory. A Python list can mix a number and a string — flexible but slow. A NumPy array forces one data type, which is exactly what makes millions of calculations fast.
Arrays වේගවත් ඇයි කිව්වොත්, ඒවා type එකක්, memory block එකක් - Shape mismatches are the #1 error you'll see in ML code. "operands could not be broadcast together" simply means two arrays don't line up in size — learning array shapes now means you'll recognise that error instantly later.
Shape mismatches තමයි ML code එකේ #1 error එක
Walkthrough
NumPy - Numerical Python
Advantages of Numpy Arrays:
- Allows several Mathematical Operations
- Faster operations
import numpy as npList vs Numpy - Time Taken
from time import process_timeTime taken by a list
python_list = [i for i in range(10000)]
start_time = process_time()
python_list = [i+5 for i in python_list]
end_time = process_time()
print(end_time - start_time)Output
0.0017722080000002194np_array = np.array([i for i in range(10000)])
start_time = process_time()
np_array += 5
end_time = process_time()
print(end_time - start_time)Output
0.0006466729999994314Numpy Arrays
# list
list1 = [1,2,3,4,5]
print(list1)
type(list1)Output
[1, 2, 3, 4, 5]Output
listnp_array = np.array([1,2,3,4,5])
print(np_array)
type(np_array)Output
[1 2 3 4 5]Output
numpy.ndarray# creating a 1 dim array
a = np.array([1,2,3,4])
print(a)Output
[1 2 3 4]a.shapeOutput
(4,)b = np.array([(1,2,3,4),(5,6,7,8)])
print(b)Output
[[1 2 3 4]
[5 6 7 8]]b.shapeOutput
(2, 4)c = np.array([(1,2,3,4),(5,6,7,8)],dtype=float)
print(c)Output
[[1. 2. 3. 4.]
[5. 6. 7. 8.]]Initial Placeholders in numpy arrays
# create a numpy array of Zeros
x = np.zeros((4,5))
print(x)Output
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]# create a numpy array of ones
y = np.ones((3,3))
print(y)Output
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]# array of a particular value
z = np.full((5,4),5)
print(z)Output
[[5 5 5 5]
[5 5 5 5]
[5 5 5 5]
[5 5 5 5]
[5 5 5 5]]# create an identity matrix
a = np.eye(5)
print(a)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.]]# create a numpy array with random values
b = np.random.random((3,4))
print(b)Output
[[0.06180442 0.35700298 0.90695408 0.03091635]
[0.58655837 0.66048197 0.29741526 0.89522409]
[0.75529751 0.8197227 0.89382064 0.85936446]]# random integer values array within a specific range
c = np.random.randint(10,100,(3,5))
print(c)Output
[[24 82 43 91 73]
[98 11 52 38 38]
[13 68 44 21 11]]# array of evenly spaced values --> specifying the number of values required
d = np.linspace(10,30,5)
print(d)Output
[10. 15. 20. 25. 30.]# array of evenly spaced values --> specifying the step
e = np.arange(10,30,5)
print(e)Output
[10 15 20 25]# convert a list to a numpy array
list2 = [10,20,20,20,50]
np_array = np.asarray(list2)
print(np_array)
type(np_array)Output
[10 20 20 20 50]Output
numpy.ndarrayAnalysing a numpy array
c = np.random.randint(10,90,(5,5))
print(c)Output
[[66 80 57 82 62]
[61 81 62 75 65]
[24 43 42 35 11]
[64 70 50 80 45]
[70 23 53 80 23]]# array dimension
print(c.shape)Output
(5, 5)# number of dimensions
print(c.ndim)Output
2# number of elements in an array
print(c.size)Output
25# checking the data type of the values in the array
print(c.dtype)Output
int64Mathematical operations on a np array
list1 = [1,2,3,4,5]
list2 = [6,7,8,9,10]
print(list1 + list2) # concatenate or joins two listOutput
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]a = np.random.randint(0,10,(3,3))
b = np.random.randint(10,20,(3,3))print(a)
print(b)Output
[[7 2 0]
[8 8 6]
[5 7 0]]
[[15 10 13]
[18 14 14]
[12 13 10]]print(a+b)
print(a-b)
print(a*b)
print(a/b)Output
[[22 12 13]
[26 22 20]
[17 20 10]]
[[ -8 -8 -13]
[-10 -6 -8]
[ -7 -6 -10]]
[[105 20 0]
[144 112 84]
[ 60 91 0]]
[[0.46666667 0.2 0. ]
[0.44444444 0.57142857 0.42857143]
[0.41666667 0.53846154 0. ]]a = np.random.randint(0,10,(3,3))
b = np.random.randint(10,20,(3,3))print(a)
print(b)Output
[[4 7 7]
[3 9 9]
[4 9 7]]
[[11 12 10]
[19 14 16]
[10 19 15]]print(np.add(a,b))
print(np.subtract(a,b))
print(np.multiply(a,b))
print(np.divide(a,b))Output
[[15 19 17]
[22 23 25]
[14 28 22]]
[[ -7 -5 -3]
[-16 -5 -7]
[ -6 -10 -8]]
[[ 44 84 70]
[ 57 126 144]
[ 40 171 105]]
[[0.36363636 0.58333333 0.7 ]
[0.15789474 0.64285714 0.5625 ]
[0.4 0.47368421 0.46666667]]Array Manipulation
array = np.random.randint(0,10,(2,3))
print(array)
print(array.shape)Output
[[1 2 2]
[8 0 7]]
(2, 3)# transpose
trans = np.transpose(array)
print(trans)
print(trans.shape)Output
[[1 8]
[2 0]
[2 7]]
(3, 2)array = np.random.randint(0,10,(2,3))
print(array)
print(array.shape)Output
[[8 1 0]
[2 5 9]]
(2, 3)trans2 = array.T
print(trans2)
print(trans2.shape)Output
[[8 2]
[1 5]
[0 9]]
(3, 2)# reshaping a array
a = np.random.randint(0,10,(2,3))
print(a)
print(a.shape)Output
[[4 3 7]
[4 6 6]]
(2, 3)b = a.reshape(3,2)
print(b)
print(b.shape)Output
[[4 3]
[7 4]
[6 6]]
(3, 2)Keep going: ← Lesson 8 · Lesson 10 → · Track it in the Self Guide
Finished this one?
Part of Self Guide · Stage 13: ML 1 — Python & Data Basics. Progress is saved in this browser.



