NUMPY
Course: Python and Numpy Library
Session #02
By: Heba-T_ALLAH
Raslan,ITI
ha1222@fayoum.edu.eg
1
Agenda
• Getting Started
✔ Course Intro
• Session Goals
• About NumPy
• Concepts
• Hands-on Exercise (1)
• Hands-on Exercise (2)
• Closing Up
• Summary
• Feedback
2
NumPy?
Explain
Why NumPy
Advantages
1D-Array & 2D-Array
Create
Empty & Zeros Array
Matrices
Session
Manipulate Different the standard data
types of NumPy
Goals (I)
UseAttributes
Functions
Arrays: indexing, slicing, reshaping.
Transform
BroadcastingRules
Traps
Manipulation
Matric
Aggregation
Axes
Session
NumPy Obj HandlingRead
Write/save
Goals
(2)
Generating Random Numbers
docstring
Help ?
??
Accessing
4
NUMPY (I)
6
• Short for Numerical Python
• NumPy is the fundamental package for
scientific computing in Python.
• Provides a multidimensional array object,
linear algebra, basic statistical
NumPy?
operations, random simulation and much more.
• Provides an efficient interface to store and
operate on dense data buffers.
various derived objects.
• Fast operations on arrays, including •
mathematical, logical, shape
6
manipulation, sorting, selecting, I/O, discrete
Fourier transforms, basic
What is the core of
the NumPy
package?
• is the ndarray object, n-dimensional
arrays of
homogeneous data types
7
What is an Array?
Central data structure
for NumPy
Grid of values Grid of elements
Elements are all of the same dtype
dimensions
The shape is a tuple of
size of the array along
each dimension.
An array can be indexed by different
ways
The `rank` is the number of
What is an Array?
Array is a linear data structure that is a collection of similar
data types. Arrays are stored in contiguous memory locations.
It is a static data structure with a fixed size. It combines data
of similar types.
9
What is an Array?
Applications of Array Data Structure
● Storing and accessing data
● Sorting
● Searching
● Matrices
● Stacks and queues
● Graphs
● Dynamic programming
10
What is an Array?
Real-Time Applications of Array:
● Signal Processing
● Multimedia Applications
● Data Mining
● Robotics
● Real-time Monitoring and Control Systems
● Financial Analysis
11
Differences between
NumPy arrays and Python Features lists?
NumPy Arrays Standard Python Sequences
Array Size Fixed size at creation Grow dynamically
Note: Changing the size of an ndarray will create a new array and delete the
original.
Data Type same
All required to be of the Different
The exception: One can have arrays of (Python, including NumPy) objects, thereby
allowing for arrays of different sized elements.
Mathematical Operations Less code More code Dealing with Large
Datasets Fast Slow
12
Vectorization
• No looping, indexing
• Just “behind the scenes” in C code.
• Advantages:
Why is
• concise and easier to read
• fewer lines means fewer bugs
• code looks like mathematical notation
NumPy Broadcasting
• Implicit element-by-element behavior of
operations • a*b
Fast?
• a and b are multidimensional arrays
11
even two arrays of with different shapes,
• the smaller array is “expandable” to the shape of
the larger.
• of the same shape, or a scalar and an array, or
Example:
-Using List with for loop -Using NumPy Library
14
Installing NumPy
•Go to the NumPy website (https://numpy.org/install/) and follow
the installation instructions.
•Once you do, you can import NumPy and double-check the
version:
12
INTERMEDIATE PYTHON FOR DATA SCIENCE
13
LET’S PRACTICE
SOME EXAMPLES
SESSION # 1 NUMPY
Topics Hands-on (I)
How to
create
basic
array
•This
section
covers
np.array(), np.zeros(), np.ones(), np.empty(), np.arange(), np.linspace(), dtype Adding, removing, and sorting elements •This section covers
np.sort(), np.concatenate()
How do you know the shape and size of an array? •This section covers ndarray.ndim, ndarray.size, ndarray.shape
Can you reshape an array? •This section covers arr.reshape()
•This section covers np.newaxis, np.expand_dims How to convert a 1D array into a 2D array (how to
add a new axis to an array)
Indexing and slicing
How to create an array from existing data •This section covers slicing and indexing, np.vstack(), np.hstack(), np.hsplit(), .view(), copy()
Basic array operations
This section covers addition, subtraction,
multiplication, division, and more
14
Practical Examples (I)
Let’s open the following notebook:
Notebook Name: S1-01-NumPy-Basics.ipynb
Notebook available on classroom“zvqwrfj“ 18
LAB 1 SUBMISSION
On “ClassRoom”
Name code:zvqwrfj
19
17
10 MIN
BREAK
https://www.online-stopwatch.c
om/
Numpy (II)
• Broadcasting
• Working with mathematical formulas
21
Broadcasting concept:
• Usually done on pairs of arrays on an element-by-element basis.
In the simplest case, the two arrays must have exactly the same shape,
as in the following example:
22
Normal NumPy Operation
• Usually done on pairs of arrays on an element-by-element basis.
In the simplest case, the two arrays must have exactly the same shape,
as in the following example:
23
NumPy broadcasting
• NumPy’s broadcasting rule relaxes this constraint when the arrays’ shapes meet certain
constraints.
The simplest broadcasting example occurs when an array and a scalar value are combined in an
operation:
24
Note That
• The result is equivalent to the previous example where b was an array.
• We can think of the scalar b being stretched during the arithmetic operation into an array with the same shape as a. • The new
elements in b are simply copies of the original scalar.
• The stretching analogy is only conceptual.
• NumPy is smart enough to use the original scalar value without actually making copies so that broadcasting operations are as
memory and computationally efficient as possible.
• The code in the second example is more efficient than that in the first because broadcasting moves less memory around during
the multiplication (b is a scalar rather than an array).
21
• It describes how NumPy treats arrays with
different shapes during arithmetic
operations.
• Like: the smaller array is “broadcast” across the
larger array so that they have compatible
shapes.
• It does this without making needless copies of data and
usually leads to efficient algorithm implementations.
Broadcasting? • There are, however, cases where
• Broadcasting provides a means of broadcasting is a bad idea because it leads
vectorizing array operations so that looping to inefficient use of memory that slows
occurs in C instead of Python. computation.
26
How
Broadcasting
Work
• When operating on two arrays,
• NumPy compares their shapes
element-wise.
• It starts with the trailing (i.e. rightmost)
dimensions and works its way left.
• Two dimensions are compatible when
• they are equal, or
• one of them is 1
• If these conditions are not met, a number of dimensions.
ValueError: operands could not be
broadca st together exception is thrown,
indicating that the arrays have incompatible 27
shapes.
• Arrays do not need to have the same
Rules of
Broadcasting
Rule 1: If the two arrays differ in their number of dimensions,
the shape of the one with fewer dimensions is padded with
ones on its leading (left) side.
Rule 2: If the shape of the two arrays does not match in any
dimension, the array with shape equal to 1 in that dimension is
stretched to match the other shape.
Rule 3: If in any dimension the sizes disagree and neither is
equal to 1, an error is raised.
28
For example,
• If you have a 256x256x3 array of RGB values, and you want to scale each
color in the image by a different value, you can multiply the image by a
one-dimensional array with 3 values.
• Lining up the sizes of the trailing axes of these arrays according to the
broadcast rules.
• Dimensions with size 1 are stretched or “copied” to match the other. 29
In the following example, both the A and B arrays have axes with
length one that are expanded to a larger size during the
broadcast operation:
30
5 examples,
31
Here are examples of shapes that do not broadcast:
32
Working with mathematical formulas
•The ease of implementing mathematical formulas that work on
arrays is one of the things that make NumPy so widely used in
the scientific Python community.
33
•For example:
• Mean square error formula
(a central formula used in supervised machine learning models that deal with regression):
• Implementing this formula is simple and straightforward in
NumPy:
34
What makes this work so well is that predictions and labels can
contain one or a thousand values. They only need to be the same size.
35
32
LET’S PRACTICE
SOME EXAMPLES
Practical Examples (II)
Let’s open the following notebook:
Notebook Name:
S1-02-Advanced-Numpy.ipynb
Notebook available on classroom“zvqwrfj“ 37
36
60 MIN
BREAK
Numpy (|||) • ِAgreggiation and Statistical Function
39
Aggregation & Statistical
Function
Definition : is a mathematical function that processes multiple input
values together to produce a single summary statistic. These functions are
commonly used in various contexts, including databases, spreadsheets, and
programming languages.
Purpose:
Aggregate functions perform calculations on sets of values and
return a single result.
They summarize data by computing statistics such as averages,
counts, maximums, minimums, and sums.
40
Aggregation & Statistical
Function
Examples :
■ Average (Arithmetic Mean): Computes the average value.
■ Count: Determines the number of elements in a set.
● Maximum: Finds the highest value.
● Median: Identifies the middle value in a sorted set.
● Minimum: Finds the lowest value.
● Mode: Determines the most frequently occurring value.
● Range: Calculates the difference between the maximum and minimum values.
● Sum: Adds up all the values.
● Nanmean: Computes the mean while ignoring NaN (missing) values.
● Std: Computes the standard deviation.
41
Aggregation & Statistical
Function
Examples :
■ Median: Identifies the middle value in a sorted set.
42
Aggregation & Statistical
Function
Examples :
● Std: Computes the standard deviation.
43
32
LET’S PRACTICE
SOME EXAMPLES
Practical Examples (II)
Let’s open the following notebook:
Notebook Name: S1-03-Array Mathematics.ipynb
Notebook available on classroom“zvqwrfj“ 45
49
CLOSING
UP
References
• https://numpy.org/doc/stable/user/whatisnumpy.html
• https://numpy.org/doc/stable/user/absolute_beginners.html#welcome-to-numpy
• https://numpy.org/doc/stable/user/basics.broadcasting.html#basics-broadcastin
g
• https://numpy.org/doc/stable/user/absolute_beginners.html#broadcasting
47
53
FEEDBACK
54
THANK YOU!