Do you want to learn about Machine learning libraries used in python and don’t know where to start, then yea you have come to the right place, to begin with, come let’s dive deep into the NumPy library which is used for machine learning with some hands-on training. Here we will begin with the basics of this library following various examples and eventually train you to a pro level.
NumPy is a Python library used for working with arrays. It also has functions for working in the domain of linear algebra, Fourier transform, and matrices. NumPy aims to provide an array object that is up to 50x faster than traditional Python lists. NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently.
In machine learning, we encounter large amounts of datasets that are handled with the help of the NumPy library.
I am using google Collaboratory to run the codes and show the outputs.
To use the library, first, we have to import the NumPy library using the below command. We are importing the library in the name of “np”.

List vs NumPy — Time taken to complete a task
Here we are creating an array of numbers to which a constant number say 5 is being added to each element of the array and the time taken to perform this task while using list and NumPy is performed and compared. The results show that NumPy accomplishes the task more quickly than lists hence it is more feasible to use NumPy while handling a larger amount of data.

Declaring a NumPy array and printing the array and its type
Here in the below code, ‘np_array’ is the variable that stores the declared NumPy array. The array is printed using the ‘print’ command and its data type is printed using the ‘type’ command which is found to be numpy.nd array (n-dimensional array).

Creating 1D and 2D array of elements
Here, 1D and a 2D array of elements are created and its shape is printed out.
The “.shape” command gives the no. of rows and columns of the input array.


Initial Place holders and matrix creation using NumPy
Initial values are present in the NumPy array. We are creating a 4×5 matrix with all the values as zero. Also, a 3×3 matrix is created with all the values as one. Following that, we create a 4×4 matrix with an array of desired values say 8 using ‘np.full()’ function which takes in two parameters out of which one is row and column size and another one is the desired value.



Create Identity Matrix
Creating an identity matrix where all the diagonal values will be 1 and other values will be zero. We use the ‘np.eye()’ function to create an identity matrix using the NumPy library. It takes in the shape of the matrix as one parameter but you should not enter the rows and column value as the identity matrix has an equal no of rows and columns.

Create a NumPy array with Random values
We use the ‘np.random.random()’ function to create a NumPy array of random values. Here in the below example, you can see the array is created with the values generated randomly within the range of 0 and 1.

In order to create an array with random integer values within a specific range we use ‘np.random.randint()’ where it takes the input ranges as a parameter followed by the shape of the matrix as another parameter.

How to create an array of evenly spaced values?
To create an array of evenly spaced values we use the function ‘np.linspace()’ which takes in the starting value and the ending values of the range as the first two parameters followed by the no of values required in the specified range as the third parameter in the function.

Another method is to use the function “np.arange()” which takes in the range and the step value for counting between the range to give the evenly spaced values as the output from the specified range. using the step value the jump count is known.

Conversion of list/tuple to NumPy array
Using ‘np.asarray()’ function list of array elements is converted into NumPy array elements.


Analyzing a NumPy array
Here we are analyzing the shape, dimensions, size, and data type of the values present in the NumPy array. The appropriate functions used are shown in the below image of the code.

Mathematical Operations on a NumPy array
When two lists are added using “+” operator mere concatenation of the lists takes place. But in the case of NumPy array addition operation is performed element-wise on the array elements. All the mathematical operations like addition, subtraction, multiplication, and division are performed by using the appropriate operators. Look on to the below code for more clarity.


Also, this can be performed using inbuilt mathematical functions as shown below.

Array Manipulation
The Transpose of an array can be found by two methods.
Method 1 is to use “np.transpose()” function and method 2 is to use array.T to find the transpose of the matrix. The code illustrates the two different ways to find the transpose of the matrix.

Reshaping an array
The below code demonstrates how to reshape a NumPy array.


If you are interested to learn more about the NumPy library functions and commands you can have a look at these websites for more content: