How to Use numpy.matmul for Matrix Multiplication
Use
numpy.matmul(a, b) to multiply two arrays or matrices following matrix multiplication rules. It works for 2D arrays as matrix multiplication and for higher dimensions as batch multiplication.Syntax
The basic syntax of numpy.matmul is:
numpy.matmul(a, b): Multiplies arraysaandbusing matrix multiplication rules.aandbcan be 1D or 2D arrays or higher dimensional arrays.- For 2D arrays, it performs standard matrix multiplication.
- For 1D arrays, it treats them as vectors and returns their dot product.
- For arrays with more than 2 dimensions, it performs batch matrix multiplication on the last two dimensions.
python
import numpy as np # Syntax example result = np.matmul(a, b)
Example
This example shows how to multiply two 2D matrices using numpy.matmul. It also shows multiplying 1D vectors.
python
import numpy as np # Define two 2D matrices matrix1 = np.array([[1, 2], [3, 4]]) matrix2 = np.array([[5, 6], [7, 8]]) # Multiply matrices result_matrix = np.matmul(matrix1, matrix2) # Define two 1D vectors vector1 = np.array([1, 2]) vector2 = np.array([3, 4]) # Multiply vectors (dot product) result_vector = np.matmul(vector1, vector2) print("Matrix multiplication result:\n", result_matrix) print("Vector multiplication (dot product) result:\n", result_vector)
Output
Matrix multiplication result:
[[19 22]
[43 50]]
Vector multiplication (dot product) result:
11
Common Pitfalls
Common mistakes when using numpy.matmul include:
- Trying to multiply arrays with incompatible shapes (the inner dimensions must match).
- Confusing element-wise multiplication (
*) with matrix multiplication (matmul). - Using 1D arrays without understanding they are treated as vectors, which affects the output shape.
Here is an example showing a shape mismatch error and the correct way:
python
import numpy as np # Wrong: incompatible shapes try: a = np.array([[1, 2, 3], [4, 5, 6]]) # shape (2,3) b = np.array([[7, 8], [9, 10]]) # shape (2,2) np.matmul(a, b) except ValueError as e: print("Error:", e) # Right: compatible shapes b_correct = np.array([[7, 8], [9, 10], [11, 12]]) # shape (3,2) result = np.matmul(a, b_correct) print("Correct multiplication result:\n", result)
Output
Error: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 3)
Correct multiplication result:
[[ 58 64]
[139 154]]
Quick Reference
Summary tips for using numpy.matmul:
- Use
matmulfor matrix multiplication, not element-wise. - Ensure inner dimensions match: if
ais (m, n),bmust be (n, p). - 1D arrays are treated as vectors; output shape depends on input shapes.
- For batch matrix multiplication, use higher dimensional arrays.
Key Takeaways
Use numpy.matmul(a, b) to perform matrix multiplication following shape rules.
Inner dimensions of arrays must match for multiplication to work.
1D arrays are treated as vectors and return dot products.
matmul is different from element-wise multiplication using * operator.
For batch operations, matmul works on the last two dimensions of arrays.