Numpy Matrix Multiplication Tutorial

Numpy Matrix Multiplication Tutorial

Numpy is a python library used for operating with large, multi-dimensional arrays and matrices. It is acknowledged to provide powerful tools and techniques to solve mathematical models of problems.

What is Numpy?

It is an open-source library and is the fundamental package for scientific computing in Python. Numpy is known as Numerical Python and known for its computations. For using it, you should have NumPy installed on your system. Otherwise, you can read how to install it from here

Matrix Multiplication

In Mathematics, Matrix multiplication is the binary operation on two matrices, resulting in the formation of one matrix. For multiplication, the number of columns of the first matrix should be equal to the second matrix’s number of rows. In the resulting matrix, its number of rows will be equal to that of the first matrix, and the number of columns will be equal the to columns of the second matrix.


How do you multiply matrices in NumPy?

NumPy includes numerous functions to perform matrix multiplication. In NumPy, the way of matrix multiplication is known as vectorisation. Vectorisation aims to reduce or remove the “for” loops used in Python to iterate over the matrix numbers.  Let us explore those functions and their different utilities- 

1. dot()

NumPy.dot() method is used to multiply two matrices in Numpy. The regular matrix multiplication involves a row multiplied to the column and added, as shown above.

In the above code,

  • We have imported the NumPy package
  • We created two arrays of dimension 3 with NumPy.array()
  • We printed the result of the NumPy.dot()

Some specifications of numpy.dot() are:

  • If both matrices A and B are 1-D, then it gives the inner product of two vectors
  • If both matrices A and B are 2-D, then it is matrix multiplication, but only if you use numpy.matmul() or A@B method
  • If either matrix A or B is scalar, it is equivalent to multiplying using NumPy

2. multiply()

In this method, element-wise multiplication is done. For Example, 

In the above code,

  • We have imported NumPy
  • We created two arrays – array1 and array2 using numpy.array() with dimension 3
  • Then, we printed the result of numpy.multiply()

3. matmul()

Matmul works similarly as dot() function. It is different from dot() in terms of scalar multiplication. You cannot multiply scalars using matmul(); you will have to use the “*” operator instead.

Its output will be – 

In the above code,

  • We imported NumPy
  • We again created two arrays using numpy.array()
  • We printed the result of multiplication with numpy.matmul()

Multiply Matrices in Python

In Python, we need to write a program to multiply matrices. It can be accomplished using two methods. Let us understand how it works – 

Method 1:

In this, we will use nested for loops to iterate through each row and column.

A = [[12, 7, 3], 
    [4, 5, 6], 
    [7, 8, 9]] 
  
B = [[5, 8, 1, 2], 
    [6, 7, 3, 0], 
    [4, 5, 9, 1]] 
      
result = [[114, 160, 60, 27], 
        [74, 97, 73, 14], 
        [119, 157, 112, 23]] 
  
for i in range(len(A)): 
 
    for j in range(len(B[0])): 
  
        for k in range(len(B)): 
            result[i][j] += A[i][k] * B[k][j] 
  
for r in result: 
    print(r) 

In the above code,

  • We have used matrix A of dimension 3*3 and matrix B of dimension 3*4.
  • We created a result matrix of dimension 3*4 to store the result of two matrices’ product. Size is 3*4 because it will have the number of rows equal to that of the first matrix and the number of columns equal to the second matrix column.
  • Next, using three for loops, we iterated through each row of matrix A and each column of matrix B, then multiplied each element of the column of matrix B with each row element of matrix A and stored the result in the resultant matrix.
  • In the end, it printed the result by iterating through the result matrix.

Method 2

In this method, we will use a nested list and zip library of Python.

A = [[12, 7, 3], 
    [4, 5, 6], 
    [7, 8, 9]] 
  
B = [[5, 8, 1, 2], 
    [6, 7, 3, 0], 
    [4, 5, 9, 1]] 
  
result = [[sum(a * b for a, b in zip(A_row, B_col))  
                        for B_col in zip(*B)] 
                                for A_row in A] 
  
for r in result: 
    print(r) 

In the above code,

  • We created two matrices of different dimensions, and the result matrix the same as in Method 1.
  • Then, we combined the list and zip method to get the result.
  • In this method also, we are using three nested loops and storing the result in the result matrix; just the code is modified using the zip method.

The output of these two methods will be equal,


[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]

Frequently Asked Questions

How do you multiply matrices in Python?

Matrices multiplication in Python is possible, as discussed above, in two ways: (a). Nested Loops and (b). Nested loops with Python library.

How do you multiply matrices in NumPy?

Vectorisation enables to multiply matrices in NumPy.

How do you multiply matrices by matrices?

So far, we have discussed how to multiply two matrices. Now, let us understand how to multiply more than two matrices –
Let A, B, and C three matrices then, according to the associative law of matrix multiplication,
ABC = (AB)C = A(BC)
First, either you can multiply two matrices AB or BC then multiply its result with the third matrix. In this way, you can multiply any number of matrices. But you need to take care of the dimensions. Matrix multiplication is not cumulative, so the order of matrices matters.

How do you multiply a vector in NumPy?

We can multiply two vectors using NumPy.dot() method. It takes two vector quantities and results in a scalar amount. The dot product, also known as the scalar product, is the product of two vectors’ magnitude and the cosine of the angle between two vectors.

Conclusion

We learned that there are three methods in NumPy that give three different results. You should choose according to what sort of result you have for output. While in the Python programme, the methods are different, but the outcome is the same. I hope it helped you to understand the concept easily.

Exit mobile version