VOOZH about

URL: https://www.geeksforgeeks.org/python/matrix-multiplication-in-numpy/

⇱ Matrix Multiplication in NumPy - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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().


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.


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