Bird
Raised Fist0
NumPydata~10 mins

Matrix multiplication with @ operator in NumPy - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Matrix multiplication with @ operator
Create Matrix A
↓
Create Matrix B
↓
Apply @ operator: A @ B
↓
Multiply rows of A by columns of B
↓
Sum products for each element
↓
Form Result Matrix
↓
Output Result Matrix
This flow shows how two matrices are created and multiplied using the @ operator, by multiplying rows of the first matrix with columns of the second and summing the products to form the result.
Execution Sample
NumPy
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
C = A @ B
print(C)
This code multiplies two 2x2 matrices A and B using the @ operator and prints the resulting matrix C.
Execution Table
StepOperationDetailsIntermediate ResultExplanation
1Create AA = [[1, 2], [3, 4]][[1, 2], [3, 4]]Matrix A is created with 2 rows and 2 columns
2Create BB = [[5, 6], [7, 8]][[5, 6], [7, 8]]Matrix B is created with 2 rows and 2 columns
3Multiply A row 1 by B col 11*5 + 2*719First element of result matrix: sum of products of first row of A and first column of B
4Multiply A row 1 by B col 21*6 + 2*822Second element of first row: sum of products of first row of A and second column of B
5Multiply A row 2 by B col 13*5 + 4*731First element of second row: sum of products of second row of A and first column of B
6Multiply A row 2 by B col 23*6 + 4*838Second element of second row: sum of products of second row of A and second column of B
7Form Result MatrixC = [[19, 22], [31, 38]][[19, 22], [31, 38]]Result matrix C formed from calculated elements
8Print COutput matrix C[[19 22] [31 38]]Final output of matrix multiplication
💡 All elements of result matrix computed; multiplication complete
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
A[[1, 2], [3, 4]][[1, 2], [3, 4]][[1, 2], [3, 4]][[1, 2], [3, 4]][[1, 2], [3, 4]][[1, 2], [3, 4]]
B[[5, 6], [7, 8]][[5, 6], [7, 8]][[5, 6], [7, 8]][[5, 6], [7, 8]][[5, 6], [7, 8]][[5, 6], [7, 8]]
CNone[[19, 0], [0, 0]][[19, 22], [0, 0]][[19, 22], [31, 0]][[19, 22], [31, 38]][[19, 22], [31, 38]]
Key Moments - 3 Insights
Why do we multiply rows of A by columns of B, not rows by rows?
Matrix multiplication requires multiplying each row of the first matrix by each column of the second matrix to get the correct dot product for each element, as shown in steps 3-6 of the execution table.
What happens if the number of columns in A does not match the number of rows in B?
The @ operator will raise an error because matrix multiplication is only defined when the inner dimensions match; this is why both A and B here are 2x2 matrices.
Why is the result matrix size 2x2?
The result matrix has the number of rows of A and the number of columns of B, so since both are 2x2, the result is also 2x2, as shown in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of C after step 4?
A[[19, 22], [0, 0]]
B[[19, 0], [0, 0]]
C[[0, 0], [0, 0]]
D[[22, 19], [0, 0]]
💡 Hint
Check the 'Intermediate Result' column at step 4 in the execution table.
At which step does the multiplication of the second row of A by the first column of B happen?
AStep 3
BStep 5
CStep 6
DStep 4
💡 Hint
Look at the 'Operation' column in the execution table for the multiplication details.
If matrix B was 3 columns wide instead of 2, how would the result matrix change?
AIt would have 3 rows
BIt would stay 2x2
CIt would have 3 columns
DMultiplication would fail
💡 Hint
Recall that the result matrix has the same number of rows as A and columns as B.
Concept Snapshot
Matrix multiplication with @ operator:
- Use A @ B to multiply matrices A and B
- Multiply rows of A by columns of B
- Sum products to get each element
- Result shape: rows of A x columns of B
- Requires inner dimensions to match
- Efficient and readable in numpy
Full Transcript
This lesson shows how to multiply two matrices using numpy's @ operator. First, two matrices A and B are created. Then, each element of the result matrix is calculated by multiplying rows of A by columns of B and summing the products. The result matrix has the number of rows of A and columns of B. The execution table traces each multiplication step and the formation of the result matrix. Key points include the need for matching inner dimensions and understanding why rows multiply columns. The visual quiz tests understanding of intermediate results and matrix dimensions.

Practice

(1/5)
1. What does the @ operator do in numpy when applied between two arrays?
easy
A. Performs matrix multiplication if shapes are compatible
B. Adds the two arrays element-wise
C. Calculates the element-wise product
D. Computes the transpose of the first array

Solution

  1. Step 1: Understand the @ operator purpose

    The @ operator in numpy is designed for matrix multiplication, which requires the inner dimensions of the two arrays to match.
  2. Step 2: Differentiate from other operations

    Element-wise addition or multiplication use + or * respectively, not @. Transpose uses .T.
  3. Final Answer:

    Performs matrix multiplication if shapes are compatible -> Option A
  4. Quick 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
A. C = A * B
B. C = A + B
C. C = A.dot(B)
D. C = A @ B

Solution

  1. Step 1: Identify the @ operator usage

    The @ operator is used as C = A @ B to perform matrix multiplication in numpy.
  2. Step 2: Differentiate from other operations

    A * B is element-wise multiplication, A.dot(B) is a method but not using @, and A + B is addition.
  3. Final Answer:

    C = A @ B -> Option D
  4. Quick 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
A. [[ 5 12] [21 32]]
B. [[ 6 8] [10 12]]
C. [[19 22] [43 50]]
D. [[ 5 6] [ 7 8]]

Solution

  1. 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) = 50
  2. Step 2: Confirm output matches calculation

    The resulting matrix is [[19, 22], [43, 50]], which matches [[19 22] [43 50]].
  3. Final Answer:

    [[19 22] [43 50]] -> Option C
  4. Quick 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
A. ValueError: shapes (2,3) and (2,2) not aligned for matrix multiplication
B. TypeError: unsupported operand type(s) for @
C. No error, output is a (2,2) matrix
D. IndexError: index out of bounds

Solution

  1. 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).
  2. Step 2: Identify mismatch and error

    Since 3 != 2, numpy raises a ValueError about shape misalignment.
  3. Final Answer:

    ValueError: shapes (2,3) and (2,2) not aligned for matrix multiplication -> Option A
  4. Quick 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:
A = np.array([[1, 0], [0, 1]])
B = np.array([[2, 3], [4, 5]])

What is the result of C = A @ B @ A?
hard
A. [[5 8] [9 14]]
B. [[2 3] [4 5]]
C. [[1 0] [0 1]]
D. [[2 4] [3 5]]

Solution

  1. 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]]
  2. Step 2: Multiply result by A again

    Multiplying B by identity matrix A again returns B:
    B @ A = B = [[2, 3], [4, 5]]
  3. Final Answer:

    [[2 3] [4 5]] -> Option B
  4. Quick 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