Matrix Multiplication with NumPy (@ and matmul)
Multiply matrices with the @ operator and see exactly how each result cell is built from a row and a column.
- The @ operator and np.matmul
- Row-times-column rule
- Shape rules: (m, n) @ (n, p)
row 0 · col 01×5 + 2×7 = 19row 0 · col 11×6 + 2×8 = 22row 1 · col 03×5 + 4×7 = 43row 1 · col 13×6 + 4×8 = 50Row times column
The problem
Matrix multiplication is behind graphics transformations, neural networks and systems of equations. It is not the same as multiplying matching numbers, and using the wrong operator gives a wrong result without any error.
Think of it like this
Matrix multiplication is like a shop totaling orders. Each customer's row lists how many of each item they bought; each price column lists what each item costs. A customer's bill is the sum of quantity × price across one row and one column.
left🧾 Rows: what each customer boughtright💲 Columns: the pricesleft @ right✖️ Row times column, then add up19 = 1×5 + 2×7🧮 One cell of the resultMatrix multiplication (@) produces a matrix whose cell [i, j] is the sum of products of row i of the left matrix and column j of the right matrix.
Key points
left @ rightperforms matrix multiplication.- The top-left result 19 is 1×5 + 2×7: row 1 of
lefttimes column 1 ofright. - The number of columns of
leftmust equal the number of rows ofright. left * rightwould instead multiply matching cells, giving a different result.
When should you use it?
- Linear algebra: transformations, rotations, systems of equations.
- Neural network layers, which are matrix multiplications.
- Combining tables of quantities and rates, such as orders and prices.
When not to use it
- Scaling or combining cell by cell: use
*. - Shapes that do not fit: (2, 3) @ (2, 3) fails; transpose one side if that is what you mean.
Example code
import numpy as np
left = np.array([[1, 2], [3, 4]])
right = np.array([[5, 6], [7, 8]])
product = left @ right
print(product)
Result of running the code
[[19 22]
[43 50]]
Code explanation
The @ operator performs matrix multiplication. It differs from the * operator, which multiplies matching elements.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input shape and dtype.
- Try negative, floating-point, and missing values.
- Apply the operation to larger multidimensional arrays and measure performance.
Common mistakes
Using * for matrix multiplication
* multiplies cell by cell and silently gives the wrong answer.
product = left * right
product = left @ right
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Matrix Multiplication Multiply two matrices, the operation behind every layer of a neural network. Easy
- Dense Layer Forward Pass Compute the outputs of a fully connected layer of neurons with a choice of activation. Medium
- Softmax Function Turn raw scores into probabilities that add up to 1, without overflowing on large values. Easy