Machine Learning
Section (3) – NumPy
What is NumPy?
NumPy is a Python library.
NumPy is short for "Numerical Python".
NumPy is a Python library used for working with arrays.
It also has functions for working in domain of linear
algebra, fourier transform, and matrices.
2
Why Use NumPy?
In Python we have lists that serve the purpose of arrays, but they
are slow to process.
NumPy aims to provide an array object that is up to 50x faster
than traditional Python lists.
The array object in NumPy is called ndarray, it provides a lot of
supporting functions that make working with ndarray very easy.
Arrays are very frequently used in data science, where speed and
resources are very important. 3
Why is NumPy Faster Than Lists?
NumPy arrays are stored at one continuous place in
memory unlike lists, so processes can access and
manipulate them very efficiently.
This behavior is called locality of reference in computer
science.
This is the main reason why NumPy is faster than lists.
Also it is optimized to work with latest CPU architectures. 4
Installation of NumPy
Install it using this command:
!pip install numpy
Now NumPy is imported and ready to use.
5
NumPy as np
NumPy is usually imported under the np alias.
alias: In Python alias are an alternate name for referring to the same thing.
Create an alias with the as keyword while importing:
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
6
NumPy vs Python Lists
Python List Operations (Concatenation, Not Element-wise Addition):
python_list = [1, 2, 3]print(python_list + python_list) # Output: [1, 2, 3, 1, 2, 3]
Lists concatenate instead of performing element-wise addition.
NumPy Array Operations (Element-wise Addition):
numpy_array = [Link]([1, 2, 3])
print(numpy_array + numpy_array) # Output: [2, 4, 6]
NumPy performs element-wise arithmetic operations.
7
Dimensions in Arrays
0-D Arrays:
arr = [Link](42)
print(arr)
1-D Arrays:
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
# 2-D Arrays:
arr = [Link]([[1, 2, 3], [4, 5, 6]])
print(arr) 8
Arrays, creation
Common NumPy Functions
Function What the function do?
[Link] Returns the shape of the array
[Link] Reshapes the array
[Link](arr) Mean of elements in array
[Link](arr) Sum of elements in array
[Link]((2,2)), [Link]((3,3)) Creating zero and one matrices
9
Arrays, creation
Creating zero and one matrices
import numpy as np
# 1D array of 5 elements, all ones
ones_array = [Link](5)
print("Ones Array:", ones_array)
# 2x3 (2 rows, 3 columns) array of zeros
zeros_array = [Link]((2, 3))
print("Zeros Array:\n", zeros_array)
10
Arrays, creation
Creates an array of random numbers :
# 1D array of 5 random numbers
random_array = [Link](5)
print("Random Array:", random_array)
# 2x3 array of random numbers
random_2d_array = [Link]((2, 3))
print("Random 2D Array:\n", random_2d_array)
11
Array Indexing
Indexing in 1-D Arrays:
arr = [Link]([10, 20, 30, 40, 50])
print(arr[0]) # First element
print(arr[-1]) # Last element
Indexing in 2-D Arrays:
arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[1, 2]) # Row 1, Column 2 (Output: 6)
12
Array Slicing
13
Array Slicing
Slicing in 1-D Arrays:
arr = [Link]([10, 20, 30, 40, 50])
print(arr[1:4]) # Elements from index 1 to 3 (Output: [20, 30, 40])
print(arr[:3]) # First three elements (Output: [10, 20, 30])
print(arr[::2]) # Every second element (Output: [10, 30, 50])
Slicing in 2-D Arrays:
arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[:, 1]) # All rows, column 1 (Output: [2, 5, 8])
print(arr[0:2, 1:3]) # Rows 0-1, Columns 1-2 (Output: [[2, 3], [5, 6]])
14
How to Transpose a Matrix in NumPy:
The transpose of a matrix flips it over its diagonal.
Rows become columns and vice versa.
15
How to Transpose a Matrix in NumPy:
# Create a 2x3 matrix
matrix = [Link]([[1, 2, 3], [4, 5, 6]])
print("Original Matrix:\n", matrix)
# Transpose the matrix
transposed_matrix = matrix.T
print("Transposed Matrix:\n", transposed_matrix)
16
Arithmetic Operations:
# 1. Arithmetic Operations are Element-Wise
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])
# Element-wise addition
print("Addition:", a + b) # [5 7 9]
# Element-wise multiplication
print("Multiplication:", a * b) # [4 10 18]
# Element-wise division
print("Division:", a / b) # [0.25 0.4 0.5] 17
Logical Operators:
# 2. Logical Operators Return a Boolean Array
# Logical comparisons
c = [Link]([10, 15, 20])
# Check if elements are greater than 12
print("Greater than 12:", c > 12) # [False True True]
# Check equality
print("Equal to 15:", c == 15) # [False True False]
# Combine logical conditions
print("Greater than 12 and less than 20:", (c > 12) & (c < 20)) # [False True False]
18
Arrays, Vectors, Matrices, Images, and
Tensors
Arrays: Structured lists of numbers stored efficiently in memory.
Vectors: 1D arrays used to represent points or directions in space.
Matrices: 2D arrays used to store tabular data or linear transformations.
Images: Represented as 2D (grayscale) or 3D (RGB) arrays.
Tensors: Multi-dimensional arrays generalizing vectors and matrices.
19
Try it out yourself
Code:
[Link]
gMvtwqeb9RqFkOMSuQoDI3JrV?usp=sharing
20
Thank you for your
attention!
21