NUMPY
NumPy can be used to perform a wide variety of mathematical operations on arrays. It adds
powerful data structures to Python that guarantee efficient calculations with arrays and
matrices
Import numpy: -
We can import numpy using the import command
import numpy
We can also use any shorthand name like ‘np’
import numpy as np
Convert a list into array: -
We can convert a list into numpy array using the [Link]() method. Converting a list into
array helps to do mathematical operations into it.
arrname = [Link](listname)
arange function: -
The arange( ) function is used to create an array between a range and with specific number
of gaps or steps in between.
arrname = [Link](start,stop,step,datatype)
The ‘start’ is inclusive but the ‘stop’ is exclusive.
Multidimensional Array: -
We can create a multidimensional array by simply using the [Link] function to a
multidimensional list.
arrname = [Link](multidimensional list)
Size function: -
By using the size function we can calculate the number of elements, number of rows,
number of columns.
• [Link] -> Number of elements
• [Link](arrname,0) -> Number of Rows
• [Link](arrname,1) -> Number of Columns
Shape function: -
The shape function returns the no of rows and columns in an array.
[Link]
No of rows No of columns
Dtype function: -
The dtype function is used to show the datatype of an array.
[Link]
Ndim function: -
The dtype function is used to show the dimension of an array whether it is one dimension
or two dimensions.
[Link]
Zeros function: -
The zeros function returns an array of specific shape and specific data type which contains
all zeroes (0).
arrname = [Link]((row,column),dtype=datatype)
Ones function: -
The ones function returns an array of specific shape and specific data type which contains
all ones (1).
arrname = [Link]((row,column),dtype=datatype)
Eyes function: -
The eyes function returns an identity array where the diagonals are all ones(1) and the
other elements are all zeroes(0).
arrname = [Link](row,column,dtype=datatype,k)
• By default, row = column but we can also give the number of columns.
• The ‘k’ value specifies where the diagonal will be with respect to the main diagonal.
▪ k>0 means above the main diagonal.
▪ k<0 means below the main diagonal.
• Example: - k = 1 means 1 place above the main diagonal and k = -1 means 1 place
below the main diagonal.
without ‘k’ k=1 k = -1
1 place above main 1 place below main
diagonal diagonal
Random functions: -
There are several random functions that we can use –
• [Link]( ) : - This function is used to return an array of given shape and
fills it with random values between 0 and 1.
arrname = [Link](row,column)
• [Link]( ) : - This function is used to return random numbers within a
specific range and we can also specify the size.
arrname = [Link](start,end,shape)
The start is inclusive and the stop is exclusive.
• [Link]( ) : - This function is used to create an array of specified shape
and fills it with random values as per standard normal distribution.
arrname = [Link](row,column)
Reshape function: -
The reshape function is used to change the shape of the array but the number of elements
should be same before and after reshaping.
Ex: - A 3X4 array has 12 elements so it can be converted to 4X3, 6X2, 2X6, 12X1 and 1X12.
arrname = [Link](row,column)
Linspace function: -
The linspace function is used to generates a number of randoms between a range. All the
values are equal distance from each other.
arrname = [Link](start,end,number of elements)
The start and end both are inclusive.
Flatten function: -
The flatten function collapses an array into a one-dimensional array.
arrname = [Link]()
Logspace function: -
The logspace function generates a number of randoms between a range but in the log
space. Ex: - if we want to generate 2 numbers between 2 and 3 then the result will be [100 ,
1000] because log 10 (100) = 2 and log 10 (1000) = 3.
arrname = [Link](start, end, no of element,base = ‘base’)
• Start and end both are inclusive.
• By default, the base is 10.
Copy function: -
The copy function is used to make a copy of an array and both the arrays will have different
ID. But if we simply assign one array to the other like – arr1 = arr2 then their ID will be
same.
arr2 = [Link](arr1)
Without using copy( ) function the By using copy( ) function the ID of
ID of both are same. both are different.
max, min, sum function: -
The max function is used to return the maximum number of a given array.
[Link]()
The min function is used to return the minimum number of a given array.
[Link]()
The sum function is used to return the sum of all numbers of a given array.
[Link]()
Here we can also set the axis value to get max, min, sum of rows or columns.
• axis = 0 means column wise.
• axis = 1 means row wise.
Overall
Row wise Column wise
Seed function: -
The seed() method is used to initialize the random number generator. If we use the same
seed value then we will get the same random number again and again.
[Link](value)
without using seed using seed
Sort function: -
The sort function is used to sort the array rowwise or column wise and by specifying the
algorithm name like quicksort, mergesort etc.
[Link](arrayname,axis,kind)
• axis = 1 (by default) means row wise sort.
• axis = 0 means column wise sort.
• kind = name of algorithm e.g. ‘mergesort’, ‘quicksort’ etc.
Mathematical operations: -
There are several mathematical functions that can be done in numpy array like –
• addition (+)
• subtraction (-)
• multiplication (*)
• division (/)
• modulus (%)
• power (**)
Dot product: -
The dot product between two arrays can be done using the ‘dot’ method.
[Link](arr2)
The dot product between two arrays can also be done using the ‘@’ method.
arr1 @ arr2
Percentile function: -
The percentile function is used to compute the nth percentile of the given data (array
elements) along the specified axis.
[Link](arr,value)
Here we can also give axis value to select row or column like previous ones.
Mean, Variance and Standard Deviation: -
The mean( ), var( ) and std( ) functions are used to calculate mean, variance and standard
deviation of an array elements respectively.
[Link]()
[Link]()
[Link]()
Filtering in a numpy array: -
We can filter in a numpy array by directly giving the condition inside the array.
arr[condition]
Transposing a numpy array: -
We can transpose a numpy array by using the ‘.T’ object. Transpose can only be done for an
array having dimensions greater than or equal to 2.
arr.T
Where clause: -
We can use the where clause to do certain things by applying any condition in an array.
[Link](condition,task,arrname)
Merging Arrays: -
We can merge two arrays in different ways and different formats –
• Merge Vertically –
▪ [Link]((arr1,arr2))
▪ [Link]((arr1, arr2), axis=0)
• Merge Horizontally –
▪ [Link]((arr1,arr2))
▪ [Link]((arr1, arr2), axis=1)
Splitting Arrays: -
We can split two arrays in different formats –
• Split Vertically –
▪ [Link]((arr1,arr2))
• Split Horizontally –
▪ [Link]((arr1,arr2))