Python Coding From Scratch: Matrix Multiplication Without Any Machine Learning Libraries!

Original article was published by Bharath K on Artificial Intelligence on Medium


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

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:

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:

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

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:

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

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.