LEARNING OBJECTIVES
•
•
•
•
•
•
•
•
•
Introduction to
NumPyNumPy Array
Creating NumPy
Array Array
Attributes
Array Methods
Array Indexing
Slicing Arrays
Array
Operation
Iteration
through Arrays
At the end of this session, you will learn:
INTRODUCTION TO NUMPY
NumPystands for ‘Numeric Python’
•
4
'Numerical Python’
Used for mathematical and scientific computations
• Also provides ‘linalg’ module which contains functions like det, eig,
norm to apply linear algebra on NumPy arrays
• NumPy array is the most widely used object of the NumPy
library
5.
INTRODUCTION TO NUMPY
•Installing NumPy
Use the following command to install Numpy using jupyter
notebook
• Importing numpy as alias np is a common
practice
5
NUMPY ARRAY
Looks similarto a
list
It is a grid of values, indexed by positive
integers
It generally contains numeric values. However it can also contain
strings
Works faster than lists because it is
homogeneous
It can be
multidimensional
7
8.
1D NUMPY ARRAY
•One dimensional array contains elements only in one dimension. In other
words, the shape of the numpy array should contain only one value in the
tuple
8
9.
2D NUMPY ARRAY
•Two dimensional array is an array within an
array
• The position of an data element is referred by two indices instead
of one
9
10.
3D NUMPY ARRAY
•A three-dimensional (3D) array is composed of 3 nested levels of arrays,
one for each dimension
10
NUMPY ARRAY OFRANDOM NUMBERS
• Create an array of 20 random numbers using random() method
from the random module
The required number of random numbers
is passed through the ‘size’ parameter
random() method returns random numbers
over the half-open interval [0.0, 1.0)
14
15.
NUMPY ARRAY OFRANDOM NUMBERS
• rand() method creates an array of random numbers of the given
shape and between (0, 1)
• The dimensions of the returned array, should all be
positive
• If no argument is given a single Python float is
returned
15
16.
NUMPY ARRAY OFRANDOM NUMBERS
• The randn() returns a set of values from the standard normal
distribution
• The dimensions of the returned array, should all be
positive
• If no argument is given a single Python float is
returned
16
17.
NUMPY ARRAY OFRANDOM NUMBERS
• The randint() returns random integers from low (inclusive) to high
(exclusive)
17
18.
CREATING NUMPY ARRAYUSING ARANGE()
• np.arange() can also be used to create a NumPy
array
• The numbers generated have the same
difference
• The function generates as many possible numbers in the given
range
Numpy.arange (start, stop, step, dtype)
The function name
The start of the
interval (optional).
Default is 0
The end of the
interval
The “step” between
values (optional)
The data type (optional)
18
19.
CREATING NUMPY ARRAYUSING ARANGE()
• The np.arange() create a series of values from 10 to 100
with a difference of 2, stored as a numpy array
19
20.
CREATING NUMPY ARRAYUSING LINSPACE()
• linspace() generates a specified number of values in a specified
range
Syntax:
numpy.linspace ( start, stop , num, dtype )
The start of the
interval
(optional)
The end of
the interval
The number
of values
required
in the
interval
The data
type
(optional
)
20
21.
CREATING NUMPY ARRAYUSING LINSPACE()
• np.linspace() produces a sequence of 10 evenly spaced values from
1 to 100, stored as a numpy array
21
22.
CREATING NUMPY ARRAYOF ZEROES
• Creating 1D numpy array of
zeros
• Creating 1D numpy array of
ones
22
23.
CREATING 2D NUMPYARRAY
• np.empty() returns the matrix with arbitrary values of given
shape and data type
• ‘dtype = object’ returns None
values
23
24.
CREATING 2D NUMPYARRAY
• np.full() returns the matrix of given shape with the value set
by the ‘fill_value’ parameter
24
25.
CREATING 2D NUMPYARRAY
• np.identity() returns the identity matrix of specified
shape
25
26.
CREATING 2D NUMPYARRAY
• np.eye() creates NxM matrix with value ‘1’ on the k-th diagonal
and remaining entries as zero
•
•
K > 0 represents upper
diagonal K < 0 represents
lower diagonal
K = 0 represents main
diagonal
K > 0 represents upper
diagonal
K < 0 represents lower
diagonal
26
ARRAY ATTRIBUTES
Some ofthe attributes of the numpy
array are:
shape size dtype
ndim
Attributes are the features/characteristics of an object that describes the object
Attributes do not have parentheses following them
28
ARRAY ATTRIBUTES-DTYPE
• Thedtype returns the type of the data along with the size in
bytes
In this example, the array consists of 64-bit floating-point numbers. Thus, the dtype of the array is
float64
31
INDEXING ARRAY
• Theelement in the array can be accessed by the positional index of the
element
• The index for an array starts at 0 from left and at -1 starts from
the right
39
ARITHMETIC OPERATIONS
• Themin() returns the minimum value present in the
array
• The max() returns the maximum value present in the
array
50
51.
ARITHMETIC OPERATIONS
• Thevar() returns the variance of all the elements in the
array
• The std() returns the standard deviation of all the elements in the
array
51
52.
ARITHMETIC OPERATIONS
• Thenp.square() returns the square of the
elements
• The np.power() is used to raise the numbers in the array to the given
value
52
CONCATENATE 1D ARRAY
•Two or more arrays will get joined along existing (first) axis, provided
they have the same shape
54
55.
CONCATENATE 2D ARRAY
•We can concatenate 2D arrays either along rows (axis = 0) or columns
(axis
= 1), provided they have same shape
55
56.
We can notconcatenate the arrays with different dimensions
56
57.
FLATTEN THE ARRAY
•The flatten() function collapses the original array into a single
dimension
57
58.
Reshape the Array
•
•
•
Reshapingmeans changing the shape of an array.
The shape of an array is the number of elements in each dimension.
By reshaping we can add or remove dimensions or change the number of
elements in each dimension.
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3
4 5 6
7 8 9
10 11 12
58
59.
Vertical Stack (vstack)
•
•
•
Youcan do vertical staking for one vector (vstack)
If you want to perform for more than one vector you want to mention in the
list
If you are vertically stacking more than one array the size of array should be
same
59
60.
Horizontal Stack (hstack)
•
•
•
Youcan do horizontal staking for one vector (hstack)
If you want to perform for more than one vector you want to mention in the list
If you are horizontally stacking more than one array the size of array may be
different not an issue
60