Lecture 4-Python-NumPy Hadi Updated
Lecture 4-Python-NumPy Hadi Updated
Lecture 4
NumPy
Source 3: Python Data Science Handbook
Based on slides prepared By: Dr. Zaher Merhi
1
What is NumpPy?
• NumPy is a Python library used for working with multidimensional
arrays.
• In Python, we have lists that serve the purpose of arrays, but they are
slow to process.
• NumPy provides an efficient multidimensional array object, called
ndarray, providing fast array-oriented arithmetic operations.
• NumPy provides mathematical functions for fast operations on entire
arrays of data without having to write loops.
2
What is NumpPy?
• It has functions for working in domain of linear algebra, Fourier
transform, and random number generation.
• Tools for reading/writing array data to disk and working with memory-
mapped files.
• It provides a C API for connecting NumPy with libraries written in C,
C++, or FORTRAN.
• NumPy is designed for efficiently handling large arrays of data.
• Arrays are very frequently used in data science, signal processing and
image processing, where speed and resources are very important.
3
NumPy array
• NumPy’s array class is called ndarray. It is also known by the alias
array.
4
NumPy array
The more important attributes of an ndarray object are:
• ndarray.ndim: the number of axes (dimensions) of the array.
• ndarray.shape: the dimensions of the array. This is a tuple of integers
indicating the size of the array in each dimension.
For a matrix with n rows and m columns, shape will be (n,m). The
length of the shape tuple is therefore the number of axes, ndim.
• ndarray.dtype: an object describing the type of the elements in the
array.
5
NumPy array
• 1D array
6
NumPy array
• 2D array
7
NumPy array
• 3D array
8
NumPy vs list: processing speed
9
NumPy ndarry: A multidimensional array object
• Generating 2D random arrays
10
NumPy ndarry: A multidimensional array object
• Nested equal length sequences, like lists, can be converter into
multidimensional arrays:
11
NumPy ndarry: A multidimensional array object
12
Array creation
13
Array Data Types
• You can specify the data type when creating an array:
14
Array Data Types
• NumPy data types
15
Element-wise Arithmetic with NumPy Arrays
16
Element-wise Arithmetic with NumPy Arrays
17
Element-wise Arithmetic with NumPy Arrays
18
Basic Indexing and Slicing
19
Basic Indexing and Slicing
• From 2D to 1 D
• 2 D indexing
20
Basic Indexing and Slicing
• 3 D Arrays
21
Index and Slicing
22
Index and Slicing
23
Fancy Indexing
• Indexing using integer arrays
24
Fancy Indexing
• Using Negative indices select rows form the end
25
Fancy Indexing
• Indexing using two lists:
26
NumPy functions
• Transpose
27
NumPy functions
• 2D matrix multiplication: it can be implemented using np.dot() or np.matmul()
28
NumPy functions
• Matrix multiplication
29
NumPy functions
• Element-wise math functions
30
NumPy functions
Element-wise
math functions
31
NumPy functions
• Element-wise maximum: Comparing two arrays
32
33
Expressing conditional Logic as Array Operations
34
Statistics
• Statistical functions are available in NumPy
35
Statistics
• We can specify along which axis the computation should be done.
36
Statistics
• Cumulative summation
37
Statistics
• Some of the statistical functions:
38
Linear Algebra
• NumPy package contains numpy.linalg module that provides most the
functionality required for linear algebra:
• Matrix inversion
• Solving systems of linear equation
• Computing eigenvectors and eigenvalues
• Computing determinant
•…
39
Linear Algebra
• Matrix inversion
40
Linear Algebra
• Eigenvalues and eigenvectors
41
Linear Algebra
42
Pseudorandom Generators
• Uniform distribution over [0.0, 1.0[
• Normal Distribution
43
Pseudorandom Generators
• Changing seed
44