0% found this document useful (0 votes)
12 views59 pages

Numpy Merged

Uploaded by

Muktai Malekar
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)
12 views59 pages

Numpy Merged

Uploaded by

Muktai Malekar
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/ 59

In this lecture

⚫Numpy

⚫C reating an array
⚫Generating arrays using built-in functions
⚫Advantages of Numpy

Python for Data Science 1


Numpy
⚫Numpy stands for numerical python
⚫Fundamental package for numerical
computations in Python
⚫Supports N -dimensional array objects
that can be used for processing multi-
dimensional data
⚫Supports different data-types

Python for Data Science 2


Numpy
⚫Using Numpy we can perform
◦ Mathematical and logical operations on arrays
◦ Fourier transforms
◦ Linear algebra operations
◦ Random number generation

Python for Data Science 3


Create an array
⚫Ordered collection of elements of basic data types of given
length
⚫Syntax:numpy.array(object)

Python for Data Science 4


Arrays
⚫Numpy can handle different categorical entities

⚫All elements are coerced to same data type

Python for Data Science 5


Generate arrays using linspace( )

• numpy.linspace()- returns equally spaced numbers within the


given range based on the sample number
• Syntax: numpy.linspace(start,stop,num,dtype,retstep)
• start - start of interval range
• stop - end of interval range
• num - number of samples to be generated
• dtype - type of output array
• retstep - return the samples, step value
Python for Data Science 6
Generate arrays using linspace( )

• Generate an array b with start=1 and stop=5

Python for Data Science 7


Generate arrays using linspace( )

• Specifying retstep=True returns samples as well the step value

Python for Data Science 8


Generate arrays using arange( )

• numpy.arange()- returns equally spaced numbers with in the


given range based on step size
• Syntax: numpy.arange(start,stop,step)
• start - start of interval range
• stop - end of interval range
• step - step size of interval

Python for Data Science 9


Generate arrays using arange( )

• Generate an array with start=1 and stop=10 by specifying


step=2

Python for Data Science 10


Generate arrays using ones( )

• numpy.ones()- returns an array of given shape and type filled


with ones
• Syntax:numpy.ones(shape,dtype)
• shape - integer or sequence of integers
• dtype - data type (default: float)

Python for Data Science 11


Generate arrays using zeros( )

• numpy.zeros()- returns an array of given shape and type


filled with zeros
• Syntax:numpy.zeros(shape,dtype)
• shape - integer or sequence of integers
• dtype - data type (default: float)

Python for Data Science 12


Generate arrays using random.rand( )

• numpy.random.rand()- returns an array of given shape filledwith random values


• Syntax:numpy.random.rand(shape)
• shape - integer or sequence of integers

Python for Data Science 13


Generate arrays using random.rand( )

• Generate an array of random values with 5 rows and 2 columns

Python for Data Science 14


Generate arrays using logspace( )

• numpy.logspace()- returns equally spaced numbers based on log


scale
• Syntax:
numpy.logspace(start,stop,num,endpoint,base,dtype)
• start - start value of the sequence
• stop - end value of the sequence
• num - number of samples to generate (default : 50)
• endpoint- if true, stop is the last sample
• base - base of the log space (default : 10.0)
• dtype- type of output array
Python for Data Science 15
Generate arrays using logspace( )

• Generate an array with 5 samples with base 10.0

Python for Data Science 16


Advantages of numpy

• Numpy supports vectorized


operations
• Array operations are carried out in
C and hence the universal functions
in numpy are faster than operations
carried out on python lists
Python for Data Science 17
Advantages of numpy- speed

• timeit module can be used to measure the execution time for


snippets of code
• Comparing the processing speed of a list and an array using
an addition operation
• Creating a list

Python for Data Science 18


Advantages of numpy- speed

• Creating a numpy array

• Note that array works faster when compared to lists

Python for Data Science 19


Advantages of numpy- storage space

• Comparing the list x and array y from the previous example to


find the memory used at the run time
• getsizeof()- returns the size of the object in bytes
• Syntax: sys.getsizeof(object)
• itemsize- returns the size if one element of a numpy array
• Syntax: numpy.ndarray.itemsize

Python for Data Science 20


Reshaping an array
⚫ reshape()- recasts an array to new shape without
changing it’s data

Python for Data Science 1


Array dimensions
⚫ Create an array a

⚫ shape()- returns dimensions of an array


⚫ Syntax: array_name.shape

Python for Data Science 2


Numpy addition
⚫ numpy.sum()- returns sum of all array elements or sum of
all array elements over a given axis
⚫ Syntax: numpy.sum(array,axis)

⚫ In the above syntax,


◦ array() - input array
◦ axis() - axis along which sum should be calculated
⚫ Create an array a

Python for Data Science 3


Numpy addition
⚫ Calculate overall sum (axis=None)

⚫ Calculate sum along the column (axis=0)

Python for Data Science 4


Numpy addition
⚫ Calculate sum along the row (axis=1)

Python for Data Science 5


Numpy addition
⚫ numpy.add()- performs elementwise addition between
two arrays
⚫ Syntax: numpy.add(array_1,array_2)

⚫ Create two arrays a and b

Python for Data Science 6


Numpy addition
⚫ Print a and b

Python for Data Science 7


Numpy multiplication
⚫ numpy.multiply()- performs elementwise multiplication
between two arrays
⚫ Syntax: numpy.multiply(array_1,array_2)

⚫ Consider the same arrays a and b

Python for Data Science 8


Numpy multiplication
⚫ Print a and b

Python for Data Science 9


Other numpy functions

Function name Description


performs element wise subtraction
numpy.subtract between two arrays
returns an element wise division of inputs
numpy.divide
Return element-wise remainder of
numpy.remainder division

Python for Data Science 10


Accessing components of an array
⚫ Components of an array can be accessed using index number
0 1 2
0 1 2 3
1 4 5 6
2 7 8 9

⚫ Extract element with index (0,1) from a

Python for Data Science 14


Accessing components of an array
⚫ Extract elements from second and third row of array a

⚫ Extract elements from first column of array a

Python for Data Science 15


Accessing components of an array
⚫ Extract elements the first row of array a

Python for Data Science 16


Modifying array using transpose ( )
⚫ numpy.transpose()- permute the dimensions of array
⚫ Syntax: numpy.transpose(array)

Python for Data Science 20


Modifying array using append( )
⚫ append()- adds values at the end of the array
⚫ Syntax: numpy.append(array,axis)

⚫ Adding the new array to a as a row

Python for Data Science 21


Modifying array using append( )
⚫ Adding the new array to a as a column
⚫ Create an array and reshape to column array

Python for Data Science 22


Modifying array using insert( )
⚫ insert()- adds values at a given position and axis in an
array
⚫ Syntax: numpy.insert(array,obj,values,axis)
◦ array - input array
◦ obj - index position
◦ values - array of values to be inserted
◦ axis - axis along which values should be insert

Python for Data Science 23


Modifying array using insert( )
⚫ Consider array a

⚫ Insert new array along row and at the 1st index position

Python for Data Science 24


Modifying array using delete( )
⚫ delete()- removes values at a given position and axis in an
array
⚫ Syntax: numpy.delete(array,obj,axis)
◦ array - input array
◦ obj - indicate array to be removed or it’s position
◦ axis - axis along which array should be removed

Python for Data Science 25


Modifying array using delete( )
⚫ Delete third row from the existing array a_ins

Python for Data Science 26


Matrices

⚫ Rectangular arrangement of numbers in rows and columns


⚫ Rows run horizontally and columns run vertically
a11 a12 a13 a11
a11 a12 a13
a21 a22 a23 a21 1x3

a31 a32 a33 a31


3x3 3x1

Python for Data Science 1


Create a matrix

⚫ matrix()- returns a matrix from an array type object or string of data


⚫ Syntax:numpy.matrix(data)

Python for Data Science 2


Matrix properties
⚫ shape()- returns number of rows and columns from a matrix

⚫ shape[0]- returns the number of rows


⚫ shape[1]- returns the number of columns

⚫ size()- returns the number of elements from a matrix

Python for Data Science 3


Modifying matrix using insert()
⚫ insert- adds values at a given position and axis in a matrix
⚫ Syntax: numpy.insert(matrix,obj,values,axis)
◦ matrix - input matrix
◦ obj - index position
◦ values - matrix of values to be inserted
◦ axis - axis along which values should be insert

Python for Data Science 4


Modifying matrix using insert()
⚫ Adding the matrix ‘col_new’ as a new column to a
⚫ Create a matrix

Python for Data Science 5


Modifying matrix using insert()
⚫ Adding the matrix ‘row_new’ as a new row to a
⚫ Create a matrix

Python for Data Science 6


Modifying matrix using index

⚫ Elements of matrix can be modified using index number


⚫ Matrix a

⚫ Here the value 1 should be updated to -3

⚫ Print the updated matrix

Python for Data Science 7


Accessing elements of matrix using index
⚫ Current matrix a

⚫ Extract elements from second row of matrix a

Python for Data Science 8


Accessing elements of matrix using index
⚫ Extract elements from third column of matrix a

⚫ Extract element with index (1,2) from a

Python for Data Science 9


Matrix addition

⚫ numpy.add()- performs elementwise addition between


two matrices
⚫ Syntax: numpy.add(matrix_1,matrix_2)

Python for Data Science 10


Matrix addition
⚫ Print A and B

Python for Data Science 11


Matrix subtraction

⚫ numpy.subtract()- performs elementwise subtraction between two matrices


⚫ Syntax:numpy.subtract(matrix_1,matrix_2)
⚫ Consider the same matrix A and B

Python for Data Science 12


Matrix subtraction
⚫ Print A and B

Python for Data Science 13


Matrix multiplication
⚫ numpy.dot()- performs matrix multiplication between two
matrices
⚫ Syntax: numpy.dot(matrix_1,matrix_2)

⚫ Consider the same matrix A and B

Python for Data Science 14


Matrix multiplication
⚫ Print A and B
For matrix multiplication,
number of columns in
matrix A should be equal to
number of rows in matrix B

Python for Data Science 15


Matrix multiplication
⚫ Transpose matrix B to make it 4x5 in dimension

Python for Data Science 16


Matrix multiplication
⚫ numpy.multiply()- performs elementwise multiplication
between two matrices
⚫ Syntax: numpy.multiply(matrix_1,matrix_2)

Python for Data Science 17


Matrix division
⚫ numpy.divide()- performs elementwise division between
two matrix
⚫ Syntax: numpy.divide(matrix_1,matrix_2)

⚫ Consider the same matrix A and B

Python for Data Science 18


Matrix division
⚫ Print A and B

Python for Data Science 19

You might also like