Numpy
Dr Yanchao Yu
Lecturer, Edinburgh Napier University
Email: [email protected]
Homepage: https://yanchao-yu.netlify.app/
Date: Jan 2022
Data Wrangling
SET11121-02
What shall we learn today?
● What is Numpy?
● Numpy Array
● Numpy Matrices
What is Numpy
What is Numpy
● NumPy is a Python C extension library for array-oriented
computing
○ widely used for numerical analysis, matrix
computations, and mathematical operations
○ Efficient
○ In-memory
○ Contiguous (or Strided)
○ Homogeneous (but types can be algebraic
What is Numpy
● NumPy is suited to many applications
○ Image processing
○ Signal processing
○ Linear algebra
○ others
NumPy is the foundation of the python
scientific stack
What is Numpy
Why do we need NumPy
● Python does numerical computations
slowly.
● 1000 x 1000 matrix multiply
○ Python triple loop takes > 10 min.
○ Numpy takes ~0.03 seconds
Numpy Array
Review of Python Types
● So far we have seen a number of Python built-in types
Review of Python Types
● Let’s take a peak at lists, which are helpful in creating 1D
and multidimensional arrays.
● A list holds a collection of objects that is ordered and
mutable. Lists are indexed and allow duplicate members.
You can add, remove, modify list elements.
Actions for Python List
❏ append()
❏ clear()
❏ copy()
❏ count()
❏ extend()
❏ index()
❏ insert()
❏ pop()
❏ remove()
❏ reverse()
● All built-in methods to manipulate lists
Code in Colab/Jupyter Notebook
Examples
What Is A Python Numpy Array?
● like Python lists, but still very much different at
the same time:
○ An array is similar to a list but it’s usually fixed
in size and has all elements of the same type.
● A NumPy array is a central data structure of the
numpy library
Arrays
Structured lists
of numbers.
▪Vectors
▪Matrices
▪Images
▪Tensors
▪ConvNets
Arrays
Structured lists
of numbers.
▪Vectors
▪Matrices
▪Images
▪Tensors
▪ConvNets
Arrays
Structured lists
of numbers.
▪Vectors
▪Matrices
▪Images
▪Tensors
▪ConvNets
Arrays
Structured lists
of numbers.
▪Vectors
▪Matrices
▪Images
▪Tensors
▪ConvNets
Arrays
Structured lists
of numbers.
▪Vectors
▪Matrices
▪Images
▪Tensors
▪ConvNets
How to use Numpy for Array?
❏ Import the NumPy module, which comes with all sorts of
methods to manipulate arrays.
❏ then cast the list
How to use Numpy for Array?
❏ Each element is of the same type, in this case integers:
Numpy Array Types
● Two levels of Types for Numpy Array:
○ type(a) → the type of the array → numpy.ndarray
○ a.dtype → the type of the values in numpy array →
dtype(‘int64’)
● Different Data Types:
○ np.uint8, np.int64, np.float32, np.float64
● Arrays are dense. Each element of the array exists and has
the same type.
Numpy Array other properties
● The attribute size is the number of elements in the array
(a.size)
● The attribute ndim represents the number of array
dimensions or the rank of the array (a.ndim)
● The attribute shape is a tuple of integers indicating the size
of the array in each dimension (a.shape)
array([0, 1, 2, 3, 4])
Numpy Assign value
Numpy Array Indexing & Slicing
x[0,0] # top-left element
x[0,-1] # first row, last column
x[0,:] # first row (many entries)
x[:,0] # first column (many entries)
Notes:
● Zero-indexing
● Multi-dimensional indices are comma-separated (i.e., a
tuple)
Numpy Array Indexing & Slicing
I[1:-1,1:-1] # select all but
one-pixel border
I = I[:,:,::-1] # swap channel order
I[I<10] = 0 # set dark pixels to black
I[[1,3], :] # select 2nd and 4th row
1. Slices are views. Writing to a slice overwrites the original
array.
2. Can also index by a list or boolean array.
NumPy Array Operations — Addition
● Between two Numpy Arrays
● Adding a constant to a NumPy array
NumPy Array Operations — Addition
NumPy Array Operations — Multiplication
● Between two Numpy Arrays
NumPy Array Operations — Multiplication
● Multiply a constant (scalar) to a NumPy Array
NumPy Array — Dot Product
NumPy Array — Mathematical Function
See more mathematics operations between Numpy Arrays:
https://numpy.org/doc/stable/reference/routines.math.html
Also called ufuncs
Element-wise
Examples:
●np.pi
●np.sqrt
●np.sin
●np.cos
NumPy Array — Mathematical Function
See more mathematics operations between Numpy Arrays:
https://numpy.org/doc/stable/reference/routines.math.html
Also called ufuncs
Element-wise
Examples:
●np.pi
●np.sqrt
●np.sin
●np.cos
NumPy Array — Mathematical Function
See more mathematics operations between Numpy Arrays:
https://numpy.org/doc/stable/reference/routines.math.html
Also called ufuncs
Element-wise
Examples:
●np.pi
●np.sqrt
●np.sin
●np.cos
NumPy Array — Mathematical Function
See more mathematics operations between Numpy Arrays:
https://numpy.org/doc/stable/reference/routines.math.html
Also called ufuncs
Element-wise
Examples:
●np.pi
●np.sqrt
●np.sin
●np.cos
Linspace
▪ A useful function for plotting mathematical functions is
“linespace”. Linespace returns evenly spaced numbers
over a specified interval.
Linspace
Linspace
Another Example of Linspace
Self-Study Tests
Please try to test yourself by answering questions in
UNIT 3 NumPy Arrays (Self-Study-01).ipynb
Numpy Matrix
Python Numpy Matrices
❏ Python doesn’t have a built-in type for matrices. However,
we can treat a list of a list as a matrix.
❏ Treat this list of a list as a matrix having 3 rows and 4
columns.
Python Numpy Matrices
❏ Consider the list a, which contains three nested lists each
one of equal size..
❏ Cast the list to a NumPy 2D Array, that is a Matrix
Accessing different elements of a NumPy Array
❏ Use rectangular brackets to access the different elements of
the array.
❏ The correspondence between the rectangular brackets and
the list and the rectangular representation is shown in the
following figure for a 3×3 array:
Accessing different elements of a NumPy Array
❏ Use simply use the square brackets and the indices
corresponding to the element
A[1, 2]
Or
A[1][2]
Accessing different elements of a NumPy Array
❏ Use simply use the square brackets and the indices
corresponding to the element
A[0, 0]
Or
A[0][0]
Accessing different elements of a NumPy Array
❏ Use slicing in NumPy arrays
❏ E.g. obtain the first two columns in the first row
A[0, 0:2]
Or
A[0][0:2]
Accessing different elements of a NumPy Array
❏ Use slicing in NumPy arrays
❏ E.g. obtain the first two columns in the first row
A[1:3, 2]
Or
A[1:3][2]
Basic Operations of Numpy Matrices
❏ The process is identical to matrix addition. Matrix addition of
X and Y
Note that: To be conformable for addition and subtraction, the operands must
have the same number of rows and columns.
Basic Operations of Numpy Matrices
❏ The process is identical to matrix addition. Matrix addition of
X and Y
Basic Operations of Numpy Matrices
❏ Multiplying a numpy array by a scalar is identical to
multiplying a matrix by a scalar.
Basic Operations of Numpy Matrices
❏ Multiplying a numpy array by a scalar is identical to
multiplying a matrix by a scalar.
Basic Operations of Numpy Matrices
❏ Multiplication of two arrays corresponds to an element-wise
product or Hadamard product
Basic Operations of Numpy Matrices
❏ Multiplication of two arrays corresponds to an element-wise
product or Hadamard product
Basic Operations of Numpy Matrices
Basic Operations of Numpy Matrices
❏ Use np.dot to perform Multiplication of two arrays
More Operations of Numpy Matrices
❏ Subtracting Matrices (A – B)
❏ Negative Matrices (-C)
❏ Matrix Transposition (A.T)
More Operations of Numpy Matrices
❏ Subtracting Matrices (A – B)
❏ Negative Matrices (-C)
❏ Matrix Transposition (A.T)
More Operations of Numpy Matrices
❏ Subtracting Matrices (A – B)
❏ Negative Matrices (-C)
❏ Matrix Transposition (A.T)
More Operations of Numpy Matrices
❏ Subtracting Matrices (A – B)
❏ Negative Matrices (-C)
❏ Matrix Transposition (A.T)
More complicated Numpy Matrix
See UNIT 3 NumPy Matrices
(LECTURE).ipynb