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

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.

4 May 20265 min readSelf Guide · Stage 13#Python & Data Basics#Python Libraries#Machine learning

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:

  1. Allows several Mathematical Operations
  2. Faster operations
ExamplePython
import numpy as np

List vs Numpy - Time Taken

ExamplePython
from time import process_time

Time taken by a list

ExamplePython
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

Output
0.0017722080000002194
ExamplePython
np_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

Output
0.0006466729999994314

Numpy Arrays

ExamplePython
# list
list1 = [1,2,3,4,5]
print(list1)
type(list1)

Output

Output
[1, 2, 3, 4, 5]

Output

Output
list
ExamplePython
np_array = np.array([1,2,3,4,5])
print(np_array)
type(np_array)

Output

Output
[1 2 3 4 5]

Output

Output
numpy.ndarray
ExamplePython
# creating a 1 dim array
a = np.array([1,2,3,4])
print(a)

Output

Output
[1 2 3 4]
ExamplePython
a.shape

Output

Output
(4,)
ExamplePython
b = np.array([(1,2,3,4),(5,6,7,8)])
print(b)

Output

Output
[[1 2 3 4]
 [5 6 7 8]]
ExamplePython
b.shape

Output

Output
(2, 4)
ExamplePython
c = np.array([(1,2,3,4),(5,6,7,8)],dtype=float)
print(c)

Output

Output
[[1. 2. 3. 4.]
 [5. 6. 7. 8.]]

Initial Placeholders in numpy arrays

ExamplePython
# create a numpy array of Zeros
x = np.zeros((4,5))
print(x)

Output

Output
[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]
ExamplePython
# create a numpy array of ones
y = np.ones((3,3))
print(y)

Output

Output
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
ExamplePython
# array of a particular value
z = np.full((5,4),5)
print(z)

Output

Output
[[5 5 5 5]
 [5 5 5 5]
 [5 5 5 5]
 [5 5 5 5]
 [5 5 5 5]]
ExamplePython
# create an identity matrix
a = np.eye(5)
print(a)

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.]]
ExamplePython
# create a numpy array with random values
b = np.random.random((3,4))
print(b)

Output

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]]
ExamplePython
# random integer values array within a specific range
c = np.random.randint(10,100,(3,5))
print(c)

Output

Output
[[24 82 43 91 73]
 [98 11 52 38 38]
 [13 68 44 21 11]]
ExamplePython
# array of evenly spaced values --> specifying the number of values required
d = np.linspace(10,30,5)
print(d)

Output

Output
[10. 15. 20. 25. 30.]
ExamplePython
# array of evenly spaced values --> specifying the step
e = np.arange(10,30,5)
print(e)

Output

Output
[10 15 20 25]
ExamplePython
# 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

Output
[10 20 20 20 50]

Output

Output
numpy.ndarray

Analysing a numpy array

ExamplePython
c = np.random.randint(10,90,(5,5))
print(c)

Output

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]]
ExamplePython
# array dimension
print(c.shape)

Output

Output
(5, 5)
ExamplePython
# number of dimensions
print(c.ndim)

Output

Output
2
ExamplePython
# number of elements in an array
print(c.size)

Output

Output
25
ExamplePython
# checking the data type of the values in the array
print(c.dtype)

Output

Output
int64

Mathematical operations on a np array

ExamplePython
list1 = [1,2,3,4,5]
list2 = [6,7,8,9,10]

print(list1 + list2)    # concatenate or joins two list

Output

Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ExamplePython
a = np.random.randint(0,10,(3,3))
b = np.random.randint(10,20,(3,3))
ExamplePython
print(a)
print(b)

Output

Output
[[7 2 0]
 [8 8 6]
 [5 7 0]]
[[15 10 13]
 [18 14 14]
 [12 13 10]]
ExamplePython
print(a+b)
print(a-b)
print(a*b)
print(a/b)

Output

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.        ]]
ExamplePython
a = np.random.randint(0,10,(3,3))
b = np.random.randint(10,20,(3,3))
ExamplePython
print(a)
print(b)

Output

Output
[[4 7 7]
 [3 9 9]
 [4 9 7]]
[[11 12 10]
 [19 14 16]
 [10 19 15]]
ExamplePython
print(np.add(a,b))
print(np.subtract(a,b))
print(np.multiply(a,b))
print(np.divide(a,b))

Output

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

ExamplePython
array = np.random.randint(0,10,(2,3))
print(array)
print(array.shape)

Output

Output
[[1 2 2]
 [8 0 7]]
(2, 3)
ExamplePython
# transpose
trans = np.transpose(array)
print(trans)
print(trans.shape)

Output

Output
[[1 8]
 [2 0]
 [2 7]]
(3, 2)
ExamplePython
array = np.random.randint(0,10,(2,3))
print(array)
print(array.shape)

Output

Output
[[8 1 0]
 [2 5 9]]
(2, 3)
ExamplePython
trans2 = array.T
print(trans2)
print(trans2.shape)

Output

Output
[[8 2]
 [1 5]
 [0 9]]
(3, 2)
ExamplePython
# reshaping a array
a = np.random.randint(0,10,(2,3))
print(a)
print(a.shape)

Output

Output
[[4 3 7]
 [4 6 6]]
(2, 3)
ExamplePython
b = a.reshape(3,2)
print(b)
print(b.shape)

Output

Output
[[4 3]
 [7 4]
 [6 6]]
(3, 2)

Keep going: ← Lesson 8 · Lesson 10 → · Track it in the Self Guide