🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.matmul()

AI & DATA SCIENCE // np-matmul

np.matmul() performs matrix multiplication between two arrays, following standard linear algebra rules, and is exactly what the @ operator calls on ndarrays.

Syntax

np.matmul(a, b)
a @ b

Deep Dive Course

For two 2D arrays, matmul() requires the number of columns in a to match the number of rows in b, and computes the standard matrix product, where each output element is the dot product of a row from a and a column from b. Unlike np.dot(), matmul() treats arrays with more than 2 dimensions as a batch of matrices stacked along the leading dimensions, broadcasting and multiplying corresponding matrix pairs — a well-defined, predictable rule that makes matmul(), or the equivalent @ operator, the recommended choice over np.dot() for anything beyond plain vectors.

1Understanding np.matmul()

For two 2D arrays, matmul() requires the number of columns in a to match the number of rows in b, and computes the standard matrix product, where each output element is the dot product of a row from a and a column from b. Unlike np.dot(), matmul() treats arrays with more than 2 dimensions as a batch of matrices stacked along the leading dimensions, broadcasting and multiplying corresponding matrix pairs — a well-defined, predictable rule that makes matmul(), or the equivalent @ operator, the recommended choice over np.dot() for anything beyond plain vectors.

💡

Use @, which calls np.matmul() internally, as your default for matrix multiplication in NumPy code — it reads cleanly, matches standard linear algebra conventions, and handles batched 3D+ matrix multiplication predictably, unlike np.dot().

editor.html
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B)
localhost:3000

2Practical Example

Here is a real-world application of np.matmul() showing how it is used in production NumPy code.

editor.html
import numpy as np

batch_A = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
batch_B = np.array([[[1, 0], [0, 1]], [[2, 0], [0, 2]]])
result = np.matmul(batch_A, batch_B)
print(result.shape)
localhost:3000

3Best Practices

Follow these guidelines when working with np.matmul():

1. Use @ (or np.matmul()) as the default for matrix multiplication, reserving * strictly for element-wise multiplication

2. Check that inner dimensions actually match, columns of the left matrix equal rows of the right, before multiplying, to catch shape errors early

3. Rely on matmul()'s batched behavior for 3D+ arrays, like multiplying a batch of matrices at once, instead of writing an explicit loop over the batch dimension

⚠️

Tip: Use @, which calls np.matmul() internally, as your default for matrix multiplication in NumPy code — it reads cleanly, matches standard linear algebra conventions, and handles batched 3D+ matrix multiplication predictably, unlike np.dot().

editor.html
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B)
Example 02Advanced Example
import numpy as np

batch_A = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
batch_B = np.array([[[1, 0], [0, 1]], [[2, 0], [0, 2]]])
result = np.matmul(batch_A, batch_B)
print(result.shape)

Best Practices

  • Use @ (or np.matmul()) as the default for matrix multiplication, reserving * strictly for element-wise multiplication
  • Check that inner dimensions actually match, columns of the left matrix equal rows of the right, before multiplying, to catch shape errors early
  • Rely on matmul()'s batched behavior for 3D+ arrays, like multiplying a batch of matrices at once, instead of writing an explicit loop over the batch dimension

Interview Question

How does np.matmul() handle two 3D arrays differently from how np.dot() would handle the same two arrays?

Hint: Think about the 'batch of matrices' interpretation.

np.matmul() treats a 3D array as a batch of 2D matrices stacked along the leading axis, and multiplies each corresponding pair of matrices from the two batches independently, producing a result that's itself a batch of matrix-multiplication results — a predictable, broadcasting-aware rule specifically designed for this use case. np.dot() applies a different, more mathematically general but less intuitive rule for higher-dimensional inputs that doesn't correspond to this simple batch-of-independent-matrix-multiplications interpretation, which is exactly why matmul() or the @ operator is the recommended, safer choice for batched matrix operations.

Exercises

MediumPractice using np.matmul() in a real scenario.
View Solution
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B)

Frequently Asked Questions

How does np.matmul() handle two 3D arrays differently from how np.dot() would handle the same two arrays?

np.matmul() treats a 3D array as a batch of 2D matrices stacked along the leading axis, and multiplies each corresponding pair of matrices from the two batches independently, producing a result that's itself a batch of matrix-multiplication results — a predictable, broadcasting-aware rule specifically designed for this use case. np.dot() applies a different, more mathematically general but less intuitive rule for higher-dimensional inputs that doesn't correspond to this simple batch-of-independent-matrix-multiplications interpretation, which is exactly why matmul() or the @ operator is the recommended, safer choice for batched matrix operations.

Related Functions

np-dotnp-multiplynp-linalg-solve