NUMPY
1
By: Asmaa Mahmoud Saeed
Asmaa Mahmoud Saeed
ITI
•
•
MSc, Computer Science
•AAST
Bachelor of Science , Menoufia University
• Computer Science
2
https://www.linkedin.com/in/asmaa-m-491750194/
Contact me
Session
Goals (I) Use
Create
Explain
Transform
Manipulate
Attributes
Functions
NumPy?
Why NumPy
Advantages
1D-Array & 2D-Array
Empty & Zeros Array
Matrices
Arrays: indexing, slicing,
reshaping.
Different the standard data
types of NumPy
3
Session
Goals (2)
Matric
Axes
Broadcasting
Accessing docstring
NumPy Obj Handling
Generating Random Numbers
Help
? ??
Rules
Traps
Read
Write/save
Manipulation
Aggregation
4
Numpy (I)
basics
21
NumPy?
6
• Short for Numerical Python
• NumPy is the fundamental package for
scientific computing in Python.
• Provides a multidimensional array object,
various derived objects.
• Fast operations on arrays, including
• mathematical, logical, shape manipulation,
sorting, selecting, I/O, discrete Fourier
transforms, basic linear algebra, basic
statistical operations, random simulation and
much more.
• Provides an efficient interface to store and
operate on dense data buffers.
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
The shape is a tuple of
size of the array along
each dimension
.
Elements are all of
the Same dtype
An array can be
indexed by different
ways
The `rank` is the
number of dimensions
8
What is an Array?
9
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.
What is an Array?
10
Applications of Array Data Structure
• Storing and accessing
• data Sorting
• Searching Matrices
• Stacks and queues Graphs
What is an Array?
11
Real-Time Applications of Array:
●Signal Processing
●Multimedia Applications
●Data Mining
● Robotics
●Real-time Monitoring and Control Systems Financial Analysis
Differences between
NumPy arrays and Python
Numpy array
Features 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
All required to be of the
same
Different
The exception: One can have arrays of (Python, including NumPy) objects, thereby
allowing for arrays of different sized elements.
Mathematical Operations
Dealing with Large Datasets
Less code More code
Fast Slow
12
Example:
14
-Using List with for loop Using NumPy Library
-
Installing NumPy
•Go to the NumPy website (https://numpy.org/install/) and follow
•Once you do, you can import NumPy and double-check the
version:
the installation instructions.
12
LET’S PRACTICE
SOME EXAMPLES
INTERMEDIATE
PYTHON
FOR
DATA
SCIENCE
SESSION
#
1
NUMPY
13
Topics Hands-on (I)
•
•This section covers arr.reshape()
•This section covers np.sort(), np.concatenate()
•This section covers np.newaxis, np.expand_dims
•This section covers ndarray.ndim, ndarray.size, ndarray.shape
•This section covers slicing and indexing, np.vstack(), np.hstack(), np.hsplit(), .view(), copy()
This section covers np.array(), np.zeros(), np.ones(), np.empty(), np.arange(), np.linspace(),
dtype
How to create a basic array
Adding, removing, and sorting
elements
How to create an array from existing
data
Basic array operations
This section covers addition, subtraction,
multiplication, division, and more
How do you know the shape and size of an
array?
Can you reshape an array?
How to convert a 1D array into a 2D array (how
to add a new axis to an array )
Indexing and slicing
1
4
Practical Examples (I)
Let’s open the following notebook:
Notebook Name: S1-01-NumPy-Basics.ipynb
18
Numpy (II)
• Broadcasting
•Working with mathematical formulas
21
Broadcasting concept:
22
•Usually done on pairs of arrays on an element-by-element basis.
In the simplest case, the two arrays must have
as in the following example:
exactly the same shape,
Normal NumPy Operation
•Usually done on pairs of arrays on an element-by-element basis.
23
In the simplest case, the two arrays must have
as in the following example:
exactly the same shape,
NumPy broadcasting
24
The simplest broadcasting example occurs when
operation:
and
•NumPy’s broadcasting rule relaxes this constraint when the arrays’ shapes meet certain
constraints.
are combined in an
an array a scalar value
Note That
21
• 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)
Broadcasting?
26
• 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.
• Broadcasting provides a means of vectorizing
array operations so that looping occurs in
C instead of Python.
• It does this without making needless copies of
data and usually leads to efficient algorithm
implementations.
• There are, however, cases where broadcasting
is a bad idea because it leads to inefficient use
of memory that slows computation.
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
ValueError: operands could not be
broadca st together exception is thrown,
indicating that the arrays have incompatible
shapes.
•Arrays do not need to have the same
number of dimensions.
27
Rules of
Broadcasting
28
Rule 3: If in any dimension the sizes disagree and neither is
equal to 1, an error is raised.
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.
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
•Implementing this formula is simple and straightforward in NumPy:
(a central formula used in supervised machine learning models that deal with regression):
34
What makes this work so well is that
contain one or a thousand values.
and can
predictions labels
They only need to be the same size.
35
LET’S PRACTICE
SOME EXAMPLES
32
Practical Examples (II)
Notebook Name: S1-02-Advanced-Numpy.ipynb
Let’s open the following notebook:
37
Numpy (|||)
•A greggiation and Statistical Function
39
Aggregation & Statistical
Function
40
Purpose:
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.
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.
Aggregation & Statistical
Function
41
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.
●
●
Aggregation & Statistical
Function
■
42
Examples :
Median: Identifies the middle value in a sorted set.
Aggregation & Statistical
Function
43
Examples :
● Std: Computes the standard deviation.
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
47
https://numpy.org/doc/stable/user/absolute_beginners.html#broadcasting
THANK YOU!
54

NumPy__data__anlysis___using__python.pdf

  • 1.
  • 2.
    Asmaa Mahmoud Saeed ITI • • MSc,Computer Science •AAST Bachelor of Science , Menoufia University • Computer Science 2 https://www.linkedin.com/in/asmaa-m-491750194/ Contact me
  • 3.
    Session Goals (I) Use Create Explain Transform Manipulate Attributes Functions NumPy? WhyNumPy Advantages 1D-Array & 2D-Array Empty & Zeros Array Matrices Arrays: indexing, slicing, reshaping. Different the standard data types of NumPy 3
  • 4.
    Session Goals (2) Matric Axes Broadcasting Accessing docstring NumPyObj Handling Generating Random Numbers Help ? ?? Rules Traps Read Write/save Manipulation Aggregation 4
  • 5.
  • 6.
    NumPy? 6 • Short forNumerical Python • NumPy is the fundamental package for scientific computing in Python. • Provides a multidimensional array object, various derived objects. • Fast operations on arrays, including • mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more. • Provides an efficient interface to store and operate on dense data buffers.
  • 7.
    What is thecore of the NumPy package? •is the ndarray object, n-dimensional arrays of homogeneous data types 7
  • 8.
    What is anArray? Central data structure for NumPy Grid of values Grid of elements The shape is a tuple of size of the array along each dimension . Elements are all of the Same dtype An array can be indexed by different ways The `rank` is the number of dimensions 8
  • 9.
    What is anArray? 9 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.
  • 10.
    What is anArray? 10 Applications of Array Data Structure • Storing and accessing • data Sorting • Searching Matrices • Stacks and queues Graphs
  • 11.
    What is anArray? 11 Real-Time Applications of Array: ●Signal Processing ●Multimedia Applications ●Data Mining ● Robotics ●Real-time Monitoring and Control Systems Financial Analysis
  • 12.
    Differences between NumPy arraysand Python Numpy array Features 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 All required to be of the same Different The exception: One can have arrays of (Python, including NumPy) objects, thereby allowing for arrays of different sized elements. Mathematical Operations Dealing with Large Datasets Less code More code Fast Slow 12
  • 13.
    Example: 14 -Using List withfor loop Using NumPy Library -
  • 14.
    Installing NumPy •Go tothe NumPy website (https://numpy.org/install/) and follow •Once you do, you can import NumPy and double-check the version: the installation instructions. 12
  • 15.
  • 16.
    Topics Hands-on (I) • •Thissection covers arr.reshape() •This section covers np.sort(), np.concatenate() •This section covers np.newaxis, np.expand_dims •This section covers ndarray.ndim, ndarray.size, ndarray.shape •This section covers slicing and indexing, np.vstack(), np.hstack(), np.hsplit(), .view(), copy() This section covers np.array(), np.zeros(), np.ones(), np.empty(), np.arange(), np.linspace(), dtype How to create a basic array Adding, removing, and sorting elements How to create an array from existing data Basic array operations This section covers addition, subtraction, multiplication, division, and more How do you know the shape and size of an array? Can you reshape an array? How to convert a 1D array into a 2D array (how to add a new axis to an array ) Indexing and slicing 1 4
  • 17.
    Practical Examples (I) Let’sopen the following notebook: Notebook Name: S1-01-NumPy-Basics.ipynb 18
  • 18.
    Numpy (II) • Broadcasting •Workingwith mathematical formulas 21
  • 19.
    Broadcasting concept: 22 •Usually doneon pairs of arrays on an element-by-element basis. In the simplest case, the two arrays must have as in the following example: exactly the same shape,
  • 20.
    Normal NumPy Operation •Usuallydone on pairs of arrays on an element-by-element basis. 23 In the simplest case, the two arrays must have as in the following example: exactly the same shape,
  • 21.
    NumPy broadcasting 24 The simplestbroadcasting example occurs when operation: and •NumPy’s broadcasting rule relaxes this constraint when the arrays’ shapes meet certain constraints. are combined in an an array a scalar value
  • 22.
    Note That 21 • Theresult 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)
  • 23.
    Broadcasting? 26 • It describeshow 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. • Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. • It does this without making needless copies of data and usually leads to efficient algorithm implementations. • There are, however, cases where broadcasting is a bad idea because it leads to inefficient use of memory that slows computation.
  • 24.
    How Broadcasting Work •When operating ontwo 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 ValueError: operands could not be broadca st together exception is thrown, indicating that the arrays have incompatible shapes. •Arrays do not need to have the same number of dimensions. 27
  • 25.
    Rules of Broadcasting 28 Rule 3:If in any dimension the sizes disagree and neither is equal to 1, an error is raised. 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.
  • 26.
    Working with mathematicalformulas •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
  • 27.
    •For example: •Mean squareerror formula •Implementing this formula is simple and straightforward in NumPy: (a central formula used in supervised machine learning models that deal with regression): 34
  • 28.
    What makes thiswork so well is that contain one or a thousand values. and can predictions labels They only need to be the same size. 35
  • 29.
  • 30.
    Practical Examples (II) NotebookName: S1-02-Advanced-Numpy.ipynb Let’s open the following notebook: 37
  • 31.
    Numpy (|||) •A greggiationand Statistical Function 39
  • 32.
    Aggregation & Statistical Function 40 Purpose: 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. 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.
  • 33.
    Aggregation & Statistical Function 41 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. ● ●
  • 34.
    Aggregation & Statistical Function ■ 42 Examples: Median: Identifies the middle value in a sorted set.
  • 35.
    Aggregation & Statistical Function 43 Examples: ● Std: Computes the standard deviation.
  • 36.
  • 37.