Matrix Multiplication in NumPy

NumPy makes matrix multiplication easy and efficient with powerful functions like dot(), matmul(), and the @ operator. Whether you're multiplying vectors, 2D matrices, or higher-dimensional arrays (tensors), NumPy offers consistent syntax and high performance.

  • Element-wise Multiplication: Multiply arrays element by element using *.
  • Matrix Multiplication: Use np.dot(), np.matmul(), or @ for standard linear algebra multiplication.

Understanding how to multiply matrices properly is essential for data science, machine learning, and numerical computing. Let's look at the different methods and when to use each.


Element-wise Multiplication

In NumPy, element-wise multiplication multiplies corresponding elements from two arrays of the same shape. This is not matrix multiplication — it’s more like multiplying values cell by cell.

You can perform element-wise multiplication using the * operator:

python
import numpy as np

# Define two arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Element-wise multiplication
result = a * b

print(result)

How It Works:

  • a and b must have the same shape (or be broadcastable).
  • a * b multiplies each element of a with the corresponding element of b.
  • The result is a new array of the same shape.
  • Example calculation:
  • result = [1*4 2*5 3*6] = [4 10 18]

Output

[ 4 10 18 ]

You can also perform element-wise multiplication on 2D arrays:

python
a = np.array([[1, 2],
                  [3, 4]])

b = np.array([[5, 6],
              [7, 8]])

result = a * b

print(result)

How It Works (2D):

  • Each element in the same position in both arrays is multiplied together.
  • Example calculation:
  • result = [[1*5, 2*6], [3*7, 4*8]] = [[5, 12], [21, 32]]
  • The output retains the same shape: (2, 2).

Output

[[ 5 12]
 [21 32]]

This is especially useful for operations like scaling, applying masks, or combining data in a pointwise fashion.

💡 Tip: For actual matrix multiplication, use np.dot(), np.matmul(), or the @ operator — we'll explore those next.


Matrix Multiplication

Unlike element-wise multiplication, matrix multiplication follows the rules of linear algebra. NumPy provides multiple ways to perform matrix multiplication, including np.dot(), np.matmul(), and the @ operator (Python 3.5+).

The number of columns in the first matrix must match the number of rows in the second matrix. The resulting matrix has the shape: (rows of A, columns of B).

python
import numpy as np

# Define two matrices
A = np.array([[1, 2],
              [3, 4]])

B = np.array([[5, 6],
              [7, 8]])

# Matrix multiplication using np.dot()
result_dot = np.dot(A, B)

# Using np.matmul()
result_matmul = np.matmul(A, B)

# Using @ operator
result_operator = A @ B

print(result_dot)
print(result_matmul)
print(result_operator)

Output

[[19 22]
 [43 50]]

How It Works:

  • Each element is computed by taking the dot product of a row from A and a column from B.
  • result[0][0] = 1×5 + 2×7 = 19
  • result[0][1] = 1×6 + 2×8 = 22
  • result[1][0] = 3×5 + 4×7 = 43
  • result[1][1] = 3×6 + 4×8 = 50

Which One Should You Use?

  • np.dot(): Works only for 1D and 2D arrays. It performs dot product for 1D and matrix multiplication for 2D, but does not support higher-dimensional arrays. Can be ambiguous.
  • np.matmul(): Explicit matrix multiplication. Supports 2D and higher-dimensional (e.g., 3D batch) arrays. Recommended for most use cases.
  • @: Clean and modern syntax for matrix multiplication (equivalent to np.matmul()). Requires Python 3.5+.

💡 Tip: Use @ or np.matmul() when you want clear matrix multiplication — especially in deep learning and numerical simulations.


Example 1: Multiplying 3×3 Matrices

In NumPy, you can multiply two 3×3 matrices using np.matmul() or the @ operator. This is a common operation in linear algebra, graphics, and scientific computing.

python
import numpy as np

# Define matrix A (3x3)
A = np.array([
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
])

# Define matrix B (3x3)
B = np.array([
  [9, 8, 7],
  [6, 5, 4],
  [3, 2, 1]
])

# Perform matrix multiplication
result = A @ B

print(result)

How It Works:

  • A has shape (3, 3): a 3×3 matrix.
  • B has shape (3, 3): another 3×3 matrix.
  • A @ B computes the matrix product, resulting in another 3×3 matrix.

Output

[[ 30  24  18]
 [ 84  69  54]
 [138 114  90]]

The resulting matrix contains the products of rows of A with columns of B, as per matrix multiplication rules.


Frequently Asked Questions

What is the difference between element-wise and matrix multiplication in NumPy?

Element-wise multiplication multiplies corresponding elements of arrays using the * operator. Matrix multiplication uses np.dot(), np.matmul(), or the @ operator and follows linear algebra rules.


Which function should I use for matrix multiplication in NumPy?

Use np.matmul() or the @ operator for clean and consistent matrix multiplication. np.dot() also works for 2D arrays but may be ambiguous with higher dimensions.


Can I multiply 3D arrays (tensors) in NumPy?

Yes, np.matmul() and the @ operator support batch matrix multiplication for 3D or higher-dimensional arrays with compatible shapes.


What happens if the shapes of the matrices are incompatible?

NumPy will raise a ValueError if the inner dimensions of the matrices do not align. For A @ B, the number of columns in A must match the number of rows in B.


Is there a performance difference between np.dot(), np.matmul(), and @?

All three methods are optimized and efficient for 2D arrays. However, np.matmul() and @ are clearer in intent and support broadcasting for tensors, making them the recommended choice.



What's Next?

Coming up next, we’ll explore the concept of the Inverse of a Matrix in NumPy — a key operation in linear algebra that's essential for solving systems of equations, performing transformations, and many applications in machine learning and scientific computing.