Num Py
Num Py
By: Heba-T_ALLAH
Raslan,ITI
[email protected]
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
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 an Array?
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?
10
What is an 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
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.
Example:
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
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
•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)
How to create an array from existing data •This section covers slicing and indexing, np.vstack(), np.hstack(), np.hsplit(), .view(), copy()
14
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
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
• 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.
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
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
39
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
41
LET’S PRACTICE
SOME EXAMPLES
Practical Examples (II)
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!