Numpy Training for Beginners
www.intellipaat.com
Linear algebra library in Python
Used for performing mathematical and logical
operations on Arrays
What is Provides features for operations
dimensional arrays and matrices in Python
on multi-
Numpy?
www.intellipaat.com
How to create Numpy Array?
www.intellipaat.com
Creating Numpy Array
1D Array
Creating
Numpy Array 2D Array
www.intellipaat.com
Ndarray Object
Most important object defined in NumPy is an N-
dimensional array type called ndarray
Describes the collection of items of the same type
What is
Items can be accessed using a zero-based index
Every item in an ndarray takes the same size of block
Numpy Array in the memory.
Each element in ndarray is an object of data-type
object (called dtype).
www.intellipaat.com
How shall I
initialize the
numpy array?
Numpy Array Initialization
www.intellipaat.com
Initializing Numpy Array
Initialize an array of ‘x’ X ‘y’ dimension with 0
Initializing
Numpy Array Arranging the numbers between x and y with an interval of z
www.intellipaat.com
Initializing Numpy Array
Arranging ‘z’ numbers between x and y
Initializing
Numpy Array
www.intellipaat.com
Initializing Numpy Array
Filling SAME number in a array of dimension x X y
Initializing
Numpy Array
Filling RANDOM numbers in a array of dimension x X y
www.intellipaat.com
How to inspect
the created
array using
numpy?
Numpy Array Inspection
www.intellipaat.com
ndarray.shape
“Returns a tuple consisting of array dimensions. Can
also be used to resize the array.”
Numpy Array For Example,
Inspection
www.intellipaat.com
ndarray.shape
“Returns a tuple consisting of array dimensions. Can
also be used to resize the array.”
Numpy Array For Example,
Inspection
www.intellipaat.com
ndarray.shape
“Returns a tuple consisting of array dimensions. Can
also be used to resize the array.”
Numpy Array For Example,
Inspection
www.intellipaat.com
ndarray.size
“Returns the count of number of elements in an array.”
Numpy Array For Example,
Inspection
www.intellipaat.com
ndarray.ndim
“Returns the dimension of the array.”
For Example,
Numpy Array
Inspection
www.intellipaat.com
ndarray.dtype
“Returns datatype of an array.”
For Example,
Numpy Array
Inspection
www.intellipaat.com
Can we
perform some
mathematical
function using
numpy?
Numpy Array Mathematics
www.intellipaat.com
Addition using Numpy
Numpy Array
Mathematics
www.intellipaat.com
Other similar operations that you can perform:
np.subtract(a,b) #a-b
np.divide(a,b) #a/b
Numpy Array np.multiply(a,b) #a*b
Mathematics np.exp(a) #e^a
np.sqrt(a)
np.sin(a)
np.cos(a)
np.log(a)
www.intellipaat.com
Array Comparison
Element-wise Comparison
Numpy Array
Mathematics
Array-wise Comparison
www.intellipaat.com
Aggregate Functions
Numpy Array
Mathematics
www.intellipaat.com
Aggregate Functions
Numpy Array
Mathematics
www.intellipaat.com
Concept of Broadcasting
Numpy
Broadcasting
www.intellipaat.com
How to select
certain
elements from
the array?
Indexing and Slicing in Python
www.intellipaat.com
Indexing in Python
“Index refers to a position .”
Numpy For Example,
Indexing and
Slicing
www.intellipaat.com
Slicing
0 1 2
0
1 2 3
Numpy 1 4 5 6
Indexing and 2 7 8 9
Slicing Let’s learn to extract/slice the array
www.intellipaat.com
Slicing
0 1 2
0
1 2 3
Numpy 1 4 5 6
Indexing and 2 7 8 9
Slicing How to extract the selected element?
My selection is in 1s t row = 0th index
A[0] ------#includes all the elements from the first row
A[:1] ------ #Extract first row from the array.
www.intellipaat.com
Slicing
0 1 2
0
1 2 3
Numpy 1 4 5 6
Indexing and 2 7 8 9
Slicing How to extract the selected element?
My selection is in 1s t row = 0th index
A[:1] ------#Extracting till row = 0 (that is 0th row)
A[:1,1:]------#Extracting till row = 0 then
select the col index starting from 1 till last
www.intellipaat.com
Slicing
0 1 2
0
1 2 3
Numpy 1 4 5 6
Indexing and 2 7 8 9
Slicing How to extract the selected element?
My selection is in 1s t two rows = 0,1 index
A[:2] ------#Extracting till row = 1 (that is 0,1)
A[:2,1:] ------ #Extracting till row = 1 (that is 0,1)
then select the col index starting from 1 till last
www.intellipaat.com
Slicing
0 1 2
0
1 2 3
Numpy 1 4 5 6
Indexing and 2 7 8 9
Slicing How to extract the selected element?
My selection is in 1s t two rows = 0,1 index
A[1:,] ------#Extracting starts from row = 1 till end
A[1:,1:] ------ #Extracting starts from row = 1 till
end then select col index = 1 till end
www.intellipaat.com
Slicing
Numpy
Indexing and
Slicing
www.intellipaat.com
What are the
various
manipulations
done within the
array?
Array Manipulation in Python
www.intellipaat.com
Concatenating two arrays together
Stack arrays row-wise(vertically)
Array
Manipulation Stack arrays column-wise(horizontally)
Combining column wise stacked array
www.intellipaat.com
Splitting Arrays
Array
Manipulation
www.intellipaat.com
Why should
I use Numpy
if I already
have a list?
Advantages of Numpy Array over List
www.intellipaat.com
Why should I
use Numpy if
I already have
a list?
Why not List?
www.intellipaat.com
What are the advantages of Numpy over List?
Advantages
of Numpy
over List
Consumes Faster More
Less Memory Convenient
www.intellipaat.com
What are the advantages of Numpy over List?
Advantages
of Numpy
over List Pointers
1 element = 4 bytes
1 Object = 14bytes
www.intellipaat.com
Numpy vs List: Memory Size
Advantages
of Numpy
over List
www.intellipaat.com
Numpy vs List: Speed
Advantages
of Numpy
over List
www.intellipaat.com
www.intellipaat.com
India : +91-7847955955
US : 1-800-216-8930 (TOLL FREE)
sales@intellipaat.com
www.intellipaat.com