Bird
Raised Fist0
NumPydata~10 mins

Matrix multiplication with @ operator in NumPy - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to multiply two matrices using the @ operator.

NumPy
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
result = A [1] B
print(result)
Drag options to blanks, or click blank then click option'
A*
B+
C-
D@
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of @ for matrix multiplication.
Using + or - operators which perform addition or subtraction.
2fill in blank
medium

Complete the code to multiply matrix A by matrix B and store the result in variable C.

NumPy
import numpy as np
A = np.array([[2, 0], [1, 3]])
B = np.array([[1, 4], [2, 5]])
C = A [1] B
print(C)
Drag options to blanks, or click blank then click option'
A//
B**
C@
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using * which multiplies elements individually.
Using // or ** which are not valid for matrix multiplication.
3fill in blank
hard

Fix the error in the code to correctly multiply matrices A and B.

NumPy
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[7, 8], [9, 10], [11, 12]])
result = A [1] B
print(result)
Drag options to blanks, or click blank then click option'
A-
B@
C+
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using * which causes shape mismatch error.
Using + or - which are invalid for matrix multiplication.
4fill in blank
hard

Fill both blanks to create a matrix multiplication and print the shape of the result.

NumPy
import numpy as np
A = np.array([[1, 0], [0, 1]])
B = np.array([[4, 1], [2, 2]])
result = A [1] B
print(result[2])
Drag options to blanks, or click blank then click option'
A@
B*
C.shape
D.size
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of @ for multiplication.
Using .size instead of .shape to get dimensions.
5fill in blank
hard

Fill all three blanks to multiply matrices and extract the element at row 1, column 0 of the result.

NumPy
import numpy as np
A = np.array([[3, 5], [7, 9]])
B = np.array([[2, 4], [6, 8]])
result = A [1] B
value = result[2][3]
print(value)
Drag options to blanks, or click blank then click option'
A@
B[1, 0]
D[1][0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of @ for multiplication.
Using a single bracket with comma inside for indexing.

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