Matrix multiplication helps combine data in rows and columns to find relationships. The @ operator makes this easy and clear.
Matrix multiplication with @ operator in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NumPy
result = matrix1 @ matrix2
The @ operator performs matrix multiplication, not element-wise multiplication.
Both matrices must have compatible shapes: columns in the first must equal rows in the second.
Examples
@.NumPy
import numpy as np A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) C = A @ B
NumPy
import numpy as np X = np.array([[1, 2, 3]]) Y = np.array([[4], [5], [6]]) Z = X @ Y
Sample Program
This program multiplies two 2x2 matrices using the @ operator and prints the input matrices and the result.
NumPy
import numpy as np # Define two matrices matrix1 = np.array([[2, 3], [4, 5]]) matrix2 = np.array([[6, 7], [8, 9]]) # Multiply using @ operator result = matrix1 @ matrix2 print("Matrix 1:") print(matrix1) print("\nMatrix 2:") print(matrix2) print("\nResult of matrix1 @ matrix2:") print(result)
Important Notes
The @ operator was introduced in Python 3.5 for matrix multiplication.
Using @ is clearer and less error-prone than using np.dot() or np.matmul().
Summary
The @ operator multiplies matrices when their shapes match.
It is simple and readable for matrix math in numpy.
Use it to combine data in rows and columns easily.
Practice
1. What does the
@ operator do in numpy when applied between two arrays?easy
Solution
Step 1: Understand the
The@operator purpose@operator in numpy is designed for matrix multiplication, which requires the inner dimensions of the two arrays to match.Step 2: Differentiate from other operations
Element-wise addition or multiplication use+or*respectively, not@. Transpose uses.T.Final Answer:
Performs matrix multiplication if shapes are compatible -> Option AQuick Check:
@means matrix multiply [OK]
Hint: Remember:
@ means matrix multiply, not element-wise [OK]Common Mistakes:
- Confusing
@with element-wise multiplication - Thinking
@adds arrays - Assuming
@transposes arrays
2. Which of the following is the correct syntax to multiply two numpy arrays
A and B using the @ operator?easy
Solution
Step 1: Identify the
The@operator usage@operator is used asC = A @ Bto perform matrix multiplication in numpy.Step 2: Differentiate from other operations
A * Bis element-wise multiplication,A.dot(B)is a method but not using@, andA + Bis addition.Final Answer:
C = A @ B-> Option DQuick Check:
Use@between arrays for matrix multiply [OK]
Hint: Use
@ directly between arrays for matrix multiply [OK]Common Mistakes:
- Using
*instead of@for matrix multiply - Confusing method
dot()with operator@ - Using addition operator
+mistakenly
3. What is the output of the following code?
import numpy as np A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) C = A @ B print(C)
medium
Solution
Step 1: Calculate matrix multiplication manually
Multiply rows of A by columns of B:
First row: (1*5 + 2*7) = 19, (1*6 + 2*8) = 22
Second row: (3*5 + 4*7) = 43, (3*6 + 4*8) = 50Step 2: Confirm output matches calculation
The resulting matrix is [[19, 22], [43, 50]], which matches [[19 22] [43 50]].Final Answer:
[[19 22] [43 50]] -> Option CQuick Check:
Matrix multiply result = [[19 22] [43 50]] [OK]
Hint: Multiply rows by columns and sum for each element [OK]
Common Mistakes:
- Adding elements instead of multiplying and summing
- Mixing element-wise multiplication with matrix multiplication
- Confusing row and column order
4. What error will occur when running this code?
import numpy as np A = np.array([[1, 2, 3], [4, 5, 6]]) B = np.array([[7, 8], [9, 10]]) C = A @ B
medium
Solution
Step 1: Check shapes of arrays
Array A shape is (2,3), array B shape is (2,2). For matrix multiplication, A's columns (3) must equal B's rows (2).Step 2: Identify mismatch and error
Since 3 != 2, numpy raises a ValueError about shape misalignment.Final Answer:
ValueError: shapes (2,3) and (2,2) not aligned for matrix multiplication -> Option AQuick Check:
Matrix multiply needs matching inner dimensions [OK]
Hint: Check inner dimensions match before using
@ [OK]Common Mistakes:
- Ignoring shape mismatch and expecting output
- Confusing element-wise multiplication with matrix multiplication
- Assuming
@works like addition
5. Given two numpy arrays:
What is the result of
A = np.array([[1, 0], [0, 1]]) B = np.array([[2, 3], [4, 5]])
What is the result of
C = A @ B @ A?hard
Solution
Step 1: Multiply A and B
Matrix A is the identity matrix. Multiplying identity with B returns B:
A @ B = B = [[2, 3], [4, 5]]Step 2: Multiply result by A again
Multiplying B by identity matrix A again returns B:
B @ A = B = [[2, 3], [4, 5]]Final Answer:
[[2 3] [4 5]] -> Option BQuick Check:
Identity matrix leaves other matrix unchanged [OK]
Hint: Identity matrix
A leaves matrix unchanged when multiplied [OK]Common Mistakes:
- Multiplying incorrectly and swapping rows/columns
- Assuming multiplication changes matrix when identity is involved
- Confusing element-wise and matrix multiplication
