NumPy

NumPy Matrix Multiplication Example

In this tutorial, we will see how to do Numpy Matrix Multiplication using NumPy library.

NumPy Multiplication:

Let’s say we have two 2-d arrays say arr1 and arr2, then if we do arr1*arr2 then it does element-wise multiplication, just like below.

import numpy as np

arr1 = np.array([[1,2,3],[1,2,3]])
arr2 = np.array([[4,5,6],[4,5,6]])

print(arr1*arr2)

Output:

[[ 4 10 18]
 [ 4 10 18]]

Above program does a simple arithmetic operation on two NumPy arrays, but it’s not exactly as the matrix multiplication, then the simple arithmetic operation won’t help us, so we have to look into the NumPy library features.

Numpy Matrix Multiplication:

In matrix multiplication, the result at each position is the sum of products of each element of the corresponding row of the first matrix with the corresponding element of the corresponding column of the second matrix. To perform matrix multiplication of 2-d arrays, NumPy defines dot operation. The dot() can be used as both a function and a method.

Let’s create a 3*3 matrix using NumPy

import numpy as np

arr1=np.arange(0,9).reshape(3,3)
print("First matrix is:")
print(arr1)
arr2=np.arange(1,10).reshape(3,3)
print("second matrix is:")
print(arr2)

Output:

First matrix is:
[[0 1 2]
 [3 4 5]
 [6 7 8]]
second matrix is:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

dot() function:

When we use dot() as a function to perform matrix multiplication of 2-d arrays arr1 and arr2, both arr1 and arr2 are passed as parameters to np.dot() function and the result is assigned to a third array arr3

import numpy as np

arr1=np.arange(0,9).reshape(3,3)
print("First matrix is:")
print(arr1)
arr2=np.arange(1,10).reshape(3,3)
print("second matrix is:")
print(arr2)

arr3=np.dot(arr1,arr2)
print("matrix product of arr1 and arr2 is:")
print(arr3)

Output:

First matrix is:
[[0 1 2]
 [3 4 5]
 [6 7 8]]
second matrix is:
[[1 2 3]
 [4 5 6]
 [7 8 9]]
matrix product of arr1 and arr2 is:
[[ 18  21  24]
 [ 54  66  78]
 [ 90 111 132]]

dot() method:

While using dot() as a method, we pass array arr2 as a parameter to arr1.dot() method.

import numpy as np

arr1=np.arange(0,9).reshape(3,3)
print("First matrix is:")
print(arr1)
arr2=np.arange(1,10).reshape(3,3)
print("second matrix is:")
print(arr2)

arr4=arr1.dot(arr2)
print("Result : ",arr4)

Output:

First matrix is:
[[0 1 2]
 [3 4 5]
 [6 7 8]]
second matrix is:
[[1 2 3]
 [4 5 6]
 [7 8 9]]
Result :  [[ 18  21  24]
 [ 54  66  78]
 [ 90 111 132]]

References:

Happy Learning :)

Found a mistake or something out of date? Tell us →

Written by

Aditya

Pythonista. Human.

Keep reading

NumPy

Arithmetic Operations on NumPy Arrays

In this tutorial, we will see how to perform basic arithmetic operations, apply trigonometric and logarithmic functions on the array elements of a NumPy array. We will also…

NumPy

How to split Numpy Arrays ?

In this tutorial, we will see how to divide an array into multiple parts. We will use the splitting operation of NumPy arrays to split arrays into rows…

NumPy

Indexing in NumPy Arrays

In this tutorial, we will see how to access elements from a numpy array with the help of indexing  to obtain the values in the arrays or assigning…

Ask a question or leave a comment