Matrix Multiplication in NumPy

Last Updated : 22 Sep, 2025

In Python, NumPy provides a way to compute matrix multiplication using numpy.dot() function. This method calculates dot product of two arrays, which is equivalent to matrix multiplication.

For example:

Suposse there are two matrices A and B.
A = [[1, 2], [2, 3]]
B = [[4, 5], [6, 7]]

So, A.B = [[1*4 + 2*6, 2*4 + 3*6], [1*5 + 2*7, 2*5 + 3*7]
Result: [[16, 26], [19, 31]]

Examples

Example 1: This example demonstrates the multiplication of two 2x2 square matrices using np.dot().

Python
import numpy as np
a = [[1, 2], [2, 3]]
b = [[4, 5], [6, 7]]

print("Matrix A:")
print(a)
print("Matrix B:")
print(b)

c = np.dot(a, b)
print("Result:")
print(c)

Output
Matrix A:
[[1, 2], [2, 3]]
Matrix B:
[[4, 5], [6, 7]]
Result:
[[16 19]
 [26 31]]

Explanation: np.dot(a, b) multiplies matrices a and b using the dot product rule:

  • First element: 14 + 26 = 16
  • Second element: 15 + 27 = 19 and so on..
  • The result is a new 2x2 matrix containing the computed products.

Example 2: This example shows multiplication of a 3x2 matrix with a 2x3 matrix, producing a 3x3 result.

Python
import numpy as np
x = [[1, 2], [2, 3], [4, 5]]
y = [[4, 5, 1], [6, 7, 2]]

print("Matrix X:")
print(x)
print("Matrix Y:")
print(y)

z = np.dot(x, y)
print("Result:")
print(z)

Output
Matrix X:
[[1, 2], [2, 3], [4, 5]]
Matrix Y:
[[4, 5, 1], [6, 7, 2]]
Result:
[[16 19  5]
 [26 31  8]
 [46 55 14]]

Explanation:

  • Each element of the resulting matrix is computed as the dot product of corresponding row from X and column from Y.
  • For example, the first element: 14 + 26 = 16, first row second column: 15 + 27 = 19, etc.
  • The result is a 3x3 matrix because a 3x2 multiplied by a 2x3 matrix gives a 3x3 matrix.
Comment