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
Recall & Review
beginner
What does the @ operator do in numpy?
The @ operator performs matrix multiplication between two numpy arrays. It multiplies rows of the first matrix by columns of the second matrix and sums the products.
Click to reveal answer
beginner
How is matrix multiplication different from element-wise multiplication?
Matrix multiplication combines rows and columns to produce a new matrix, while element-wise multiplication multiplies corresponding elements directly without combining rows and columns.
Click to reveal answer
intermediate
Given two matrices A of shape (2, 3) and B of shape (3, 4), what will be the shape of A @ B?
The result will have shape (2, 4) because the inner dimensions (3) match and the output shape is the outer dimensions (2 from A and 4 from B).
Click to reveal answer
intermediate
What error occurs if you try to multiply matrices with incompatible shapes using @?
You get a ValueError saying shapes are not aligned because the number of columns in the first matrix must equal the number of rows in the second matrix.
Click to reveal answer
beginner
Write a simple numpy code snippet to multiply two matrices A and B using the @ operator.
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
result = A @ B
print(result)
Click to reveal answer
What does the @ operator do in numpy?
AAdds two matrices
BPerforms element-wise multiplication
CSubtracts two matrices
DPerforms matrix multiplication
✗ Incorrect
The @ operator is used for matrix multiplication in numpy.
If matrix A has shape (3, 2) and matrix B has shape (2, 4), what is the shape of A @ B?
A(2, 2)
B(3, 4)
C(3, 2)
D(4, 3)
✗ Incorrect
The output shape is (3, 4) because the inner dimensions 2 match and outer dimensions are 3 and 4.
What happens if you try to multiply matrices with shapes (2, 3) and (2, 4) using @?
AIt works and returns shape (2, 4)
BIt returns a scalar
CIt raises a ValueError due to shape mismatch
DIt performs element-wise multiplication
✗ Incorrect
Matrix multiplication requires the number of columns in the first matrix to equal the number of rows in the second matrix.
Which numpy function is equivalent to using the @ operator?
Anp.dot()
Bnp.multiply()
Cnp.add()
Dnp.cross()
✗ Incorrect
np.dot() performs matrix multiplication, same as the @ operator.
What is the result of multiplying a (2, 2) identity matrix with another (2, 2) matrix using @?
AThe other matrix unchanged
BA zero matrix
CA matrix of ones
DAn error
✗ Incorrect
Multiplying by the identity matrix returns the other matrix unchanged.
Explain how the @ operator works for matrix multiplication in numpy.
Think about how you multiply matrices by hand.
You got /4 concepts.
Describe what happens if you try to multiply two numpy arrays with incompatible shapes using the @ operator.
Consider the rules for matrix multiplication dimensions.
You got /3 concepts.
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
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.
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 A
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
Step 1: Identify the @ operator usage
The @ operator is used as C = A @ B to perform matrix multiplication in numpy.
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.
Final Answer:
C = A @ B -> Option D
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
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
Step 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 C
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
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 A