DATA ANALYSIS
5 NumPy Functions
Numpy stands for Numerical Python .Numpy is an open source library which is used for numerical operations with respect to linear algebra and matrices .It is created by Travis Oliphant in 2005 .
5 Numpy Functions
- numpy.sort
- numpy.argmax
- numpy.count_nonzero
- numpy.mat
- numpy.flip
First we will import numpy:
Function 1 — np.sort
Sorting means putting something in a sequence . Numpy comes with an inbuilt library which will allows us to sort an arrray in ascending order and retrurn us a copy of sorted array without modifying the actual instance .
numpy.sort(a, axis=-1, kind=None, order=None)
In this example the print statement will return the sorted version of an array (arr) along with the default axis and along with the first axis .
In this example we are sorting an array alphabatically . Here it captures the binary value of the first element to sort so you can see in the above example ‘Mango’ is the first element to be sorted
Function 2 — np.argmax
np.argmax returns the index which consists of the maximum value in an array with respect to the specified axis .
numpy.argmax(a, axis=None, out=None)
In the first print statement no axis is mentioned so it will work with the entire array ,and the second print statement will return the maximum element as per the axis .
In this example we are using np.arange to give a range within which we will get out array and we are specifying the shape of the array with respect to the range .
Function 3 — np.count_nonzero
As the name suggests it will count the no. of non zero elements in an array .
numpy.count_nonzero(a, axis=None, *, keepdims=False)
In this example we have give a (2,4) matrix and we need to identify the no. of non zero elements
In this example np.eye() represents identity matrix , an identify matrix is the one which consists of ones in the diagonals and rest of the elements are zeros
Function 4 — np.mat
np.mat converts input any kind of input and interpret it as matrix
numpy.mat(data, dtype=None)
Here we are taking a list of lenght 2 and converting it into a (2,2) matrix .
Here we have taken a tuple of len 5 and convered it into a (1,5) matrix.
Function 5 — np.flip
Takes elements as input convert it into array and then print it in a reverse order .
numpy.flip(m, axis=None)
Here we are converting a list of length 6 to a (6,) length ndarray then displaying it in a reverse order .
Here we have specified the axis about which we want to flip our array .
Conclusion
In this notebook you will find 5 useful numpy functions and 2 refrenced examples of every function .
Reference Links
- Numpy official tutorial : https://numpy.org/doc/stable/user/quickstart.html
- Used GeeksforGeeks for some extra refrence :https://www.geeksforgeeks.org/