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
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
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
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
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.
●
●