Slot06 Numpy1
Slot06 Numpy1
Slot 06-
Numpy – Part 01
Advisor:
Dr. Nguyễn Tiến Huy
Dr. Lê Thanh Tùng
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 1
Numpy
▪ NumPy is a Python library used for working with arrays
▪ One of the most important foundational packages for numerical
computing in Python.
▪ Most computational packages providing scientific functionality
use NumPy’s array objects as the lingua franca for data
exchange.
▪ Providing fast array-oriented operations and flexible
broadcasting capabilities
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 2
Array
▪ NumPy is used to work with arrays. The array object in NumPy is
called ndarray
▪ Create a NumPy ndarray object by using the array() function
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 3
Array
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 4
Dimensions in Arrays
▪ A dimension in arrays is one level of array depth (nested arrays)
▪ 0-D array or Scalars, are the elements in an array
▪ 1-D array has 0-D arrays as its elements, called uni-dimensional
▪ 2-D array has 1-D arrays as its elements, called 2-d matrix
▪ 3-D array has 2-D matrices as its elements
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 5
Dimensions in Arrays
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 6
Shape & Type of ndarray
▪ Attribute: ndarray.shape – Tuple of array dimensions
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 7
Create Array
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 8
dtype
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 9
Type & Type cast
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 10
numpy.arange()
▪ arange([start,] stop[, step,][, dtype])
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 11
numpy.arange()
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 12
numpy.random.randn
▪ random.randn(d0, d1, ..., dn): Return a sample (or samples) from the
“standard normal” distribution.
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 13
numpy.random.randn
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 14
Numerical operations on arrays
▪ Elementwise operations:
▪ With scalars:
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 15
Numerical operations on arrays
▪ Elementwise operations: With array (same dimension):
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 16
Numerical operations on arrays
▪ Matrix multiplication
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 17
Numerical operations on arrays
Comparisons:
Array-wise
Comparisons:
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 18
Numerical operations on arrays
▪ Logical operations
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 19
List vs numpy array in Math operation
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 20
Indexing & Slicing
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 21
Indexing & Slicing
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 22
Indexing & Slicing
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 23
Indexing & Slicing
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 24
Indexing & Slicing
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 25
Copies & Views in ndarray
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 26
Copies & Views in ndarray
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 27
Boolean indexing
▪ Numpy arrays support a feature called conditional selection to generate
a new array of boolean values that state whether each element within the
array satisfies a particular if statement.
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 28
Boolean indexing
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 29
Boolean indexing
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 30
Fancy Index
▪ Besides using indexing & slicing, NumPy provides you with a convenient
way to index an array called fancy indexing.
▪ Fancy indexing allows you to index a numpy array using the following:
▪ Another numpy array
▪ A Python list
▪ A sequence of integers
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 31
Fancy Index
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 32
Fancy Index
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 33
Reshape
▪ Reshaping means changing the shape of an array
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 34
Reshape
▪ Reshaping returns the view of the original array
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 35
Reshape: Unknown Dimension
▪ Pass -1 as the value, and NumPy will calculate this number for you
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 36
Exercise
▪ Ex1: Given an integer matrix of NxM, set negative values in the
matrix to 0
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 37
Exercise
def setZero4Negative_Index(arr):
shape = arr.shape
for row in range(shape[0]):
for col in range(shape[1]):
if (arr[row][col] < 0):
arr[row][col] = 0
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 39
Exercise
def setZero4Negative_Index(arr):
shape = arr.shape
for row in range(shape[0]):
for col in range(shape[1]):
if (row % 2 == 0):
if (arr[row][col] < 0):
arr[row][col] = 0
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 40
Exercise
def setZero4Negative(arr):
index = arr < 0
for i in range(len(arr)): # range(arr.shape[0])
if (i % 2 == 0): arr[i][index[i]] = 0
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 41
Exercise
arr3 = np.array([[1, 2, -3], [-1, -2, 3], [-3, -2, 4]])
arrEvenRow = arr3[::2]
arrEvenRow[arrEvenRow < 0] = 0
print(arr3)
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 42
Exercise
Ex3: Create a checkboard matrix NxN as follows:
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 43
Exercise
def createMatrix(n: int):
arr = np.zeros((n, n), dtype = np.int64)
mark = 0
for row in range(n):
for col in range(n):
arr[row][col] = mark
mark = not(mark)
mark = not(mark)
return arr
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 44
Exercise
def createMatrixVer2(n: int):
arr = np.zeros((n, n), dtype = np.int64)
arr[1::2, ::2] = 1
arr[::2, 1::2] = 1
return arr
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 45
Exercise
Ex4: Create a matrix NxM with 1 in borders and 0 for inside:
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 46
Exercise
def createMatrix(n: int, m: int):
arr = np.zeros((n, m), dtype = np.int64)
for row in range(n):
for col in range(n):
if (row == 0 or row == n-1):
arr[row][col] = 1
if (col == 0 or col == m - 1):
arr[row][col] = 1
return arr
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 47
Exercise
def createMatrixVer2(n: int, m: int):
arr = np.zeros((n, m), dtype = np.int64)
arr[0,:] = 1
arr[n-1,:] = 1
arr[:, 0] = 1
arr[:, m-1] = 1
return arr
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 48
Exercise
def createMatrixVer3(n: int, m: int):
arr = np.ones((n, m), dtype = np.int64)
arr[1:-1, 1:-1] = 0
return arr
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 49
Exercise
Ex5: Create a NxN matrix as follows:
N=4
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 50
Exercise
def createMatrix(n: int):
content = list(range(n*n))
arr = np.array(content)
arr = arr.reshape(n, -1)
for row in range(n):
for col in range(n):
if (col == row):
arr[row][col] = 100
return arr
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 51
Exercise
def createMatrixVer2(n: int):
arr = np.arange(n*n)
arr[0::5] = 100
return arr.reshape(n, -1)
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 52
Exercise
Ex6: Write a function to multiply two matrices NxN without using
np.dot()
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 53
Exercise
def matrix_multiply(a, b):
n = len(a)
c = np.array([[0 for i in range(n)] for j in range(n)])
for i in range(n):
for j in range(n):
for k in range(n):
c[i][j] += a[i][k] * b[k][j]
return c
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 54
Exercise
def matrixMul(a, b):
return a@b
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 55
THANK YOU
for YOUR ATTENTION
Huy Tien Nguyen – Tung Le MTH083 - Advanced Programming for Artificial Intelligence Page 56