Previously we have seen the introduction of NumPy and some of the basics of NumPy along with the installation. Now we will see some more basics to understand more functionality of the NumPy module.
The identity matrix is a type of matrix in which all the elements of the principal diagonal matrix are one, and all the value other than a diagonal value are zero.
To initialize the identity matrix eye() function is used in NumPy. The eye function takes only one parameter which is used as both rows and columns because for identity matrix it is compulsory that the matrix should be square.
Following is the method to initialize the identity matrix:
If we run the above code following output will be generated:
In the above function, we use eye function and give it value seven as a parameter, and in return, an array is initialized having seven rows and seven columns. All the value in diagonal are one, and all the values other than diagonal are given the value of zero.
Some Basics of NumPy:
- ndarray.ndim: It returns the number of axis or dimension of an array. It tells whether an array is one dimensional, two dimensional or n-dimensional array.
- ndarray.shape: This function will return the size means the number of columns and the number of rows will be returned. The rows and column will be returned as a tuple value.
- ndarray.size: By using this function, the total number of elements in an array will be returned.
- ndarray.dtype: This function will describe the type of element in the array.
The coding implementation of the above functionality is as follows. Suppose we have initialized the one's array of 3 dimension now we will check all the above functionality one by one in our coding editor:
The output of the above example is as follows:
Here we can see that dimensions are 3, rows columns concerning dimensions are (3,4,5). If we multiple 3x4x5 we get the 60, so the total number of elements in an array is 60 and data type of each element is a float.
Suppose we have a task to generate n numbers between two given numbers. In actual it is a long task, but NumPy makes it very easy. Suppose we want to get ten numbers between zero and one so NumPy linespace method will create an array of ten elements starting from zero and ending to one and in between there will be equal intervals.
Following is the code for linespace:
The above written is the code for the linspace method. It takes three parameters. The parameter is where to start, the second one is the ending point, and the third one is the number of intervals. The variable took ten intervals, and variable b took seven intervals.
Following is the output generated by this code.
The first output generates ten intervals and second output generates seven intervals.
Reshaping method is used to convert or to manipulate the rows and columns of the NumPy array. This function is mostly used when doing image processing to change the number of pixels or when subjected to neural network than reshaping is used to make the image acceptable for the network.
Following is the code for reshaping:
After importing the module, we make a variable and, in that variable, we initialize the NumPy array of 1 row and 15 columns. Then we reshape it by five rows and three columns,
The output is as follows:
We can do all sort of mathematical operations in NumPy like addition, subtraction, multiplication, square, exponential, dot matrix, square root, etc.
Following are the example code of doing mathematical operation using NumPy which otherwise could be a lengthy process when data is massive.
The output of the above code is as follows: