Multiply two arrays python without numpy

Understanding how to implement matrix multiplication without any machine learning libraries from scratch!

Multiply two arrays python without numpy

Photo by Dhru J on Unsplash

Have you ever imagined working on machine learning problems without any of the sophisticated awesome machine learning libraries?

Thanks to these modules, we have certain operations that are almost done within the blink of the eye.

To truly appreciate the beauty and elegance of these modules let us code matrix multiplication from scratch without any machine learning libraries or modules. Although this is not an extremely complicated task, this will help us learn the core concepts better and also understand the significance of NumPy, which can complete the same task in just a few lines of code.

So, without further ado, let us get our hands dirty and begin coding!

My approach to this problem is going to be to take all the inputs from the user. These are the number of rows and columns of both the first and second matrix. Also, based on the number of rows and columns of each matrix, we will respectively fill the alternative positions accordingly.

The first step, before doing any matrix multiplication is to check if this operation between the two matrices is actually possible. This can be done by checking if the columns of the first matrix matches the shape of the rows in the second matrix. This can be formulated as:

→ no. of columns in matrix 1 = no. of rows in matrix 2

Using this strategy, we can formulate our first code block. This can be done as shown below —

Alright, this part was pretty simple. We formulated a plan to perform the matrix operation only when desired. Now, let us look at how to receive the inputs for the respective rows and columns accordingly.

Before moving on, let us formulate a question that we are trying to solve. The below image represents the question we have to solve. I took an easier 3*3 and 3*3 combination of matrices, but I promise this method will work for any complicated problem with matching columns of the 1st matrix to matching rows of the 2nd matrix.

Multiply two arrays python without numpy

Screenshot By Author

The below image represents a look at the respective number of rows and columns.

Multiply two arrays python without numpy

Now that we have formulated our problem statement as well, let us take the desired inputs from the users and start working on solving this problem. This can be done from the below code block:

Here, I have shown how to iterate across the rows and columns to input the values for the first matrix. Similarly, you can repeat the steps for the second matrix as well. After completing this step your output should look as follows:

Multiply two arrays python without numpy

Okay, so now we have successfully taken all the required inputs. It is time to loop across these values and start computing them. So, just to clarify how matrix multiplication works, you multiply the rows with their respective columns.

The first Value of the matrix must be as follows:

(1*1) + (2*4) + (3 * 7) = (1) + (8) + (21) = 30

This can be done using the following code:

This code computes the result accordingly, and we get the final output as follows:

Multiply two arrays python without numpy

Below is the figure to show the same calculation which was completed.

Multiply two arrays python without numpy

Multiply two arrays python without numpy

Multiply two arrays python without numpy

Screenshots By Author

Ok Awesome! We completed working with the matrices now. However, I am curious to see how would this would work on numpy. Let us have a look 👀

Working with Numpy:

After successfully formatting the working of matrix multiplication using only python we can now look at how a similar formulation with numpy module would look like. This can be done as follows:

Multiply two arrays python without numpy

Welp! Looks like that is all we had to ever do.

Multiply two arrays python without numpy

Gif from Clipart

That was almost no work whatsoever, and here I sat coding this in Python. Well! At least we learned something new and can now appreciate how wonderful the machine learning libraries we use are.

Photo by Antoine Dautry on Unsplash

Conclusion:

We figured out that without using the amazing machine learning libraries that exist, even a simple task like matrix multiplication, which could be done otherwise in barely a few lines of code, will take a longer time to execute.

However, that being said, it is still important to understand the core basics and understanding of how these operations are performed, and we did exactly that in this article. In this article, we looked at how to code matrix multiplication without using any libraries whatsoever.

If you want me to do more of this “Python Coding Without Machine Learning Libraries.” then please feel free to suggest any more ideas you would expect me to try out in the upcoming articles.

You can check out my most recent articles with the below links:

Feel free to check out the article series that will cover the entire mastery of machine learning from scratch below. The series will be updated consistently, and this series will cover every topic and algorithm related to machine learning with python from scratch.

Thank you all for reading this article, and I wish you all a wonderful day!

How do you multiply two matrices in Python without NumPy?

To multiply a matrix we use a nested for loop. Nested for loop is a for loop inside another for loop. For the above code, we have given our own set of values, then we have initialized the resultant matrix res to 0 and iterated in a set of for loops.

How do you multiply arrays in Python?

multiply() function is used when we want to compute the multiplication of two array. It returns the product of arr1 and arr2, element-wise.

How do you multiply 2d arrays in Python?

Step1: input two matrix. Step 2: nested for loops to iterate through each row and each column. Step 3: take one resultant matrix which is initially contains all 0. Then we multiply each row elements of first matrix with each elements of second matrix, then add all multiplied value.

Can you multiply two arrays?

C = A . * B multiplies arrays A and B by multiplying corresponding elements. The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.