Numpy
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
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 = np.arange(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 np.array function to a
multidimensional list.
arrname = np.array(multidimensional list)
Size function: -
By using the size function we can calculate the number of elements, number of rows,
number of columns.
Shape function: -
The shape function returns the no of rows and columns in an array.
arrname.shape
No of rows No of columns
Dtype function: -
The dtype function is used to show the datatype of an array.
arrname.dtype
Ndim function: -
The dtype function is used to show the dimension of an array whether it is one dimension
or two dimensions.
arrname.ndim
Zeros function: -
The zeros function returns an array of specific shape and specific data type which contains
all zeroes (0).
arrname = np.zeros((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 = np.ones((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 = np.eye(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.
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 = array.linspace(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 = array.flatten()
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 = np.logspace(start, end, no of element,base = ‘base’)
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 = np.copy(arr1)
Overall
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.
np.sort(arrayname,axis,kind)
• addition (+)
• subtraction (-)
• multiplication (*)
• division (/)
• modulus (%)
• power (**)
Dot product: -
The dot product between two arrays can be done using the ‘dot’ method.
arr1.dot(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.
np.percentile(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.
arr.mean()
arr.var()
arr.std()
Where clause: -
We can use the where clause to do certain things by applying any condition in an array.
np.where(condition,task,arrname)
Merging Arrays: -
We can merge two arrays in different ways and different formats –
• Merge Vertically –
▪ np.vstack((arr1,arr2))
▪ np.concatenate((arr1, arr2), axis=0)
• Merge Horizontally –
▪ np.hstack((arr1,arr2))
▪ np.concatenate((arr1, arr2), axis=1)
Splitting Arrays: -
We can split two arrays in different formats –
• Split Vertically –
▪ np.vsplit((arr1,arr2))
• Split Horizontally –
▪ np.hsplit((arr1,arr2))