KEMBAR78
Numpyfunctioninpythonforadvanceddata.pptx
Python Programming
NumP
y
LEARNING OBJECTIVES
•
•
•
•
•
•
•
•
•
Introduction to
NumPy NumPy 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
INTRODUCTION TO NUMPY
NumPy stands 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
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
NUMPY ARRAY
Looks similar to 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
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
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
3D NUMPY ARRAY
• A three-dimensional (3D) array is composed of 3 nested levels of arrays,
one for each dimension
10
Creating NumPy array
CONVERTING A LIST INTO NUMPY ARRAY
• np.array() is used to create a numpy array from a
list
12
CREATING NUMPY ARRAY
• Numpy arrays be used to create array of strings as
well
13
NUMPY ARRAY OF RANDOM 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
NUMPY ARRAY OF RANDOM 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
NUMPY ARRAY OF RANDOM 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
NUMPY ARRAY OF RANDOM NUMBERS
• The randint() returns random integers from low (inclusive) to high
(exclusive)
17
CREATING NUMPY ARRAY USING 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
CREATING NUMPY ARRAY USING ARANGE()
• The np.arange() create a series of values from 10 to 100
with a difference of 2, stored as a numpy array
19
CREATING NUMPY ARRAY USING 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
CREATING NUMPY ARRAY USING LINSPACE()
• np.linspace() produces a sequence of 10 evenly spaced values from
1 to 100, stored as a numpy array
21
CREATING NUMPY ARRAY OF ZEROES
• Creating 1D numpy array of
zeros
• Creating 1D numpy array of
ones
22
CREATING 2D NUMPY ARRAY
• np.empty() returns the matrix with arbitrary values of given
shape and data type
• ‘dtype = object’ returns None
values
23
CREATING 2D NUMPY ARRAY
• np.full() returns the matrix of given shape with the value set
by the ‘fill_value’ parameter
24
CREATING 2D NUMPY ARRAY
• np.identity() returns the identity matrix of specified
shape
25
CREATING 2D NUMPY ARRAY
• 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
ARRAY ATTRIBUTES
Some of the 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-SHAPE
• The shape returns the number of rows and columns of the array
respectively
29
ARRAY ATTRIBUTES-SIZE
• The size returns the number of elements in an
array
30
ARRAY ATTRIBUTES-DTYPE
• The dtype 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
ARRAY ATTRIBUTES-NDIM
• The ndim returns the number of axes (dimension) of the
array
32
ARRAY ATTRIBUTES-NDIM
33
Array Methods
ARRAY METHODS
Methods are object functions that takes
parameters in the parentheses and returns
the modified object
35
ARRAY METHODS
• In this example, the reshape(6, 1) is reshaping a 3 X 2 array to 6 X 1
array
36
Array Indexing
INDEXING ARRAY
• The element 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
Slicing
Array
SLICING ARRAYS
40
SLICING 1D ARRAY
• Slicing allows us to access more than one
element
41
SLICING 2D ARRAY
• Slicing in 2d array returns a sub-matrix of the original
matrix
42
The index returns an element of the array, the slice returns a list of elements.
43
If you try to add arrays with different dimensions, you get an error.
44
Array Operations
ARITHMETIC OPERATIONS
Addition and Subtraction of 1D
array
46
ARITHMETIC OPERATIONS
Multiply each element in the array
by 2
47
ARITHMETIC OPERATIONS
Element-wise multiplication of two 3x3
matrices
m
1
m
2
m1
*
m2
This product is also known as ‘Hadamard Product’
48
ARITHMETIC OPERATIONS
Matrix multiplication of two 3x3
matrices
49
ARITHMETIC OPERATIONS
• The min() returns the minimum value present in the
array
• The max() returns the maximum value present in the
array
50
ARITHMETIC OPERATIONS
• The var() returns the variance of all the elements in the
array
• The std() returns the standard deviation of all the elements in the
array
51
ARITHMETIC OPERATIONS
• The np.square() returns the square of the
elements
• The np.power() is used to raise the numbers in the array to the given
value
52
ARITHMETIC OPERATIONS
• The np.transpose() reverses the dimension of the
array
53
CONCATENATE 1D ARRAY
• Two or more arrays will get joined along existing (first) axis, provided
they have the same shape
54
CONCATENATE 2D ARRAY
• We can concatenate 2D arrays either along rows (axis = 0) or columns
(axis
= 1), provided they have same shape
55
We can not concatenate the arrays with different dimensions
56
FLATTEN THE ARRAY
• The flatten() function collapses the original array into a single
dimension
57
Reshape the Array
•
•
•
Reshaping means 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
Vertical Stack (vstack)
•
•
•
You can 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
Horizontal Stack (hstack)
•
•
•
You can 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
Iterating through
Arrays
ITERATING THROUGH 1D ARRAY
• The for loop can be used to iterate through the array
elements
62
ITERATING THROUGH 2D ARRAY
63
ITERATING THROUGH 2D ARRAY USING NESTED FOR LOOP
• To print each element in the 2D array, use nested for
loop
64
NumPy arrays are faster than list. The below code shows that NumPy arrays are very much
faster than the lists in python.
65

Numpyfunctioninpythonforadvanceddata.pptx

  • 1.
  • 2.
    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:
  • 3.
  • 4.
    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
  • 6.
  • 7.
    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
  • 11.
  • 12.
    CONVERTING A LISTINTO NUMPY ARRAY • np.array() is used to create a numpy array from a list 12
  • 13.
    CREATING NUMPY ARRAY •Numpy arrays be used to create array of strings as well 13
  • 14.
    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
  • 27.
  • 28.
    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
  • 29.
    ARRAY ATTRIBUTES-SHAPE • Theshape returns the number of rows and columns of the array respectively 29
  • 30.
    ARRAY ATTRIBUTES-SIZE • Thesize returns the number of elements in an array 30
  • 31.
    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
  • 32.
    ARRAY ATTRIBUTES-NDIM • Thendim returns the number of axes (dimension) of the array 32
  • 33.
  • 34.
  • 35.
    ARRAY METHODS Methods areobject functions that takes parameters in the parentheses and returns the modified object 35
  • 36.
    ARRAY METHODS • Inthis example, the reshape(6, 1) is reshaping a 3 X 2 array to 6 X 1 array 36
  • 37.
  • 38.
    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
  • 39.
  • 40.
  • 41.
    SLICING 1D ARRAY •Slicing allows us to access more than one element 41
  • 42.
    SLICING 2D ARRAY •Slicing in 2d array returns a sub-matrix of the original matrix 42
  • 43.
    The index returnsan element of the array, the slice returns a list of elements. 43
  • 44.
    If you tryto add arrays with different dimensions, you get an error. 44
  • 45.
  • 46.
    ARITHMETIC OPERATIONS Addition andSubtraction of 1D array 46
  • 47.
    ARITHMETIC OPERATIONS Multiply eachelement in the array by 2 47
  • 48.
    ARITHMETIC OPERATIONS Element-wise multiplicationof two 3x3 matrices m 1 m 2 m1 * m2 This product is also known as ‘Hadamard Product’ 48
  • 49.
  • 50.
    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
  • 53.
    ARITHMETIC OPERATIONS • Thenp.transpose() reverses the dimension of the array 53
  • 54.
    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
  • 61.
  • 62.
    ITERATING THROUGH 1DARRAY • The for loop can be used to iterate through the array elements 62
  • 63.
  • 64.
    ITERATING THROUGH 2DARRAY USING NESTED FOR LOOP • To print each element in the 2D array, use nested for loop 64
  • 65.
    NumPy arrays arefaster than list. The below code shows that NumPy arrays are very much faster than the lists in python. 65