[go: up one dir, main page]

0% found this document useful (0 votes)
32 views47 pages

Numpy

The document provides an overview of Numpy, a key library for handling N-dimensional arrays in Python, covering its basic concepts, array creation methods, and functionalities such as stacking, comparison, and broadcasting. It also explains how to save and load Numpy arrays using NPZ files and discusses the concept of sparse matrices. The session aims to equip learners with foundational skills in manipulating and processing data using Numpy.

Uploaded by

mepengineer42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views47 pages

Numpy

The document provides an overview of Numpy, a key library for handling N-dimensional arrays in Python, covering its basic concepts, array creation methods, and functionalities such as stacking, comparison, and broadcasting. It also explains how to save and load Numpy arrays using NPZ files and discusses the concept of sparse matrices. The session aims to equip learners with foundational skills in manipulating and processing data using Numpy.

Uploaded by

mepengineer42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Machine Learning Diploma

Session 2: Numpy
Agenda:
1 Numpy Basics
2 Quick Array Creation
3 NPZ Files
4 Stacking
5 Comparison
6 Conditional Access & Modification
7 Array BroadCasting
8 Sparse Matrices
1. Numpy Basics
What is Numpy?
➢ One of the main primarily used data structures to
represent & process data.
➢ Numpy is about creating N-dimensional Tensors, these
Tensors are called Numpy Arrays or ndarray.
➢ Numpy arrays could be 0-dimensional(scalars), 1-
dimensional(vectors), 2-dimensional(matrices), 3-
dimensional(Images), etc.
Import:
Create Numpy Arrays:
Dimensions & shape:
Reshape:

Add Dimension:
Dtype:
➢ Numpy array can only carry one data type.
➢ If you pass elements with different datatypes, Numpy will
change all of them into one datatype (the most general
dtype).
➢ Here is the order of general datatypes:
➢ Object > String > Float > Int > Bool.
Dtype examples:
Change Dtypes:
➢ Numpy allow you to Change the Datatype of ndarrays.
➢ Examples:
Indexing:
➢ Means accessing one element in Numpy array using its index.
Slicing:
➢ Means accessing many elements in an array using index range.
Transopose:
➢ Make matrix columns become rows and rows become
columns.
2. Quick Array Creation
Quick arrays Methods :
➢ Numpy provides you some built-in methods that helps you
create arrays quickly.
➢ In the coming slides, you will find the most popular
methods.
Zeros():
➢ Creates an array of the specified size with the contents
filled with zero values.
Ones():
➢ Creates an array of the specified size with the contents
filled with one values.
Full():
➢ Creates a new array of the specified shape with the
contents filled with a specified value.
Empty():
➢ Allocates space of a numpy array in the memory without
initializing array elements values.

➢ Use it when you want to create arrays quickly if you plan


to fill them with meaningful values later.
arange():
➢ Generates values starting from a given start value,
incrementing by a step.
➢ It takes three parameters; (start, stop, step).
Linspace():
➢ Is used to create an array of evenly spaced values within a
specified range, For example, np.linspace(0, 10, num=20) will
create an array of 20 evenly spaced values between 0 and 10,
including both 0 and 10.
Linspace():
➢ To create 10X10 Matrix using Linspace:
Identity ():
➢ Creates an identity matrix with a given shape.
Random.random():
➢ Creates an array of a given shape with random values
between 0 & 1.
Random.randint():
➢ Creates an array of a given shape with random values
between a & b, where a & b are boundaries that you
specify.
Random.choice():
➢ Creates an array of a given shape with random values
sampled from elements of another array.
3. NPZ files
What are NPZ files?
➢ Numpy provides a way to save your numpy arrays as files
called npz files which helps you to save the data you want
in npz format.
➢ You can save the files using a built-in method called savez,
and you can load using a built-in method called load.
Save Numpy arrays: Load Numpy arrays:
4. Stacking
What Is Stacking?
➢ Stacking means Concatenating two matrices together.
➢ There are two types of Stacking:
Vertical Stacking Horizontal Stacking
➢ The two matrices must have ➢ The two matrices must have
the same number of columns the same number of rows.
Why Stacking?
➢ Sometimes you collect datasets from different sources, and
you might want to concatenate them into one dataset.
Vertical Stacking Horizontal Stacking
5. Comparison
Comparison using ‘==’:
➢ Is about elementwise comparison between two numpy arrays.
➢ The result is a Boolean numpy array with the same shape.
➢ Examples:
Comparison using all():
➢ Returns True if the all elements follow the condition

Comparison using any():


➢ Returns True if any element follows the condition
Comparison using isclose():
➢ It applies elementwise comparison with tolerance, where
we check if every element is equal or close to its
corresponding element in another array.
➢ In the following example, tolerance = 1.
Comparison using allclose():
➢ It applies comparison with tolerance, where we check if all
elements in an array is equal or close to its corresponding
element in another array.
➢ In the following example, tolerance = 1.
6. Conditional Access & Modification
Conditional Access:
➢ It means getting elements of an array that satisfy a specific
condition.
➢ A Condition means the result of elementwise comparison, we
saw in the previous section.
Condition Conditional Access
Conditional Modification using:
➢ It means applying modification to elements of an array that
satisfy a specific condition.
Condition Conditional Modification Conditional Modification
using ‘==’ using where()
7. Array BroadCasting
Array BroadCasting:
➢ Is the application of arithmetic operations between arrays
with a different shape.
Vector/Scalar BroadCasting Matrix/Vector BroadCasting
8. Sparse Matrices
Sparse Matrix:
➢ Sparse matrix is a matrix that contain mostly zero values.
➢ The opposite of sparse matrix is dense matrix, which is a
matrix where most of the values are non-zero.
➢ Sparse matrices has a problem related to waste of memory
resources as those zero values do not contain any
information.
Sparse Matrix:
➢ The solution is to transform it into another data structure.
Where the zero values can be ignored.
➢ There are two techniques:

➢ These two techniques are doing the same thing, but in


different ways, which we are not interested in.
Thank You

You might also like