0
0
NumPydata~20 mins

Matrix multiplication with @ operator in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Matrix Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this matrix multiplication?
Given two numpy arrays A and B, what is the result of A @ B?
NumPy
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[2, 0], [1, 2]])
result = A @ B
print(result)
A
[[4 2]
 [7 8]]
B
[[4 4]
 [10 8]]
C
[[2 4]
 [3 8]]
D
[[2 0]
 [1 2]]
Attempts:
2 left
💡 Hint
Remember matrix multiplication sums the products of rows of A with columns of B.
data_output
intermediate
1:30remaining
What is the shape of the result after matrix multiplication?
If A has shape (3, 4) and B has shape (4, 2), what is the shape of A @ B?
NumPy
import numpy as np
A = np.zeros((3,4))
B = np.zeros((4,2))
result = A @ B
print(result.shape)
A(3, 4)
B(4, 4)
C(2, 3)
D(3, 2)
Attempts:
2 left
💡 Hint
The resulting matrix shape is rows of A by columns of B.
🔧 Debug
advanced
2:00remaining
Why does this matrix multiplication raise an error?
What error will this code raise and why?
NumPy
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[7, 8], [9, 10]])
result = A @ B
AIndexError: index out of bounds
BTypeError: unsupported operand type(s) for @: 'list' and 'ndarray'
CValueError: shapes (2,3) and (2,2) not aligned: 3 (dim 1) != 2 (dim 0)
DNo error, outputs a (2,2) matrix
Attempts:
2 left
💡 Hint
Check if the inner dimensions of the matrices match for multiplication.
🚀 Application
advanced
2:30remaining
Calculate the product of a matrix and its transpose
Given matrix M, what is the result of M @ M.T?
NumPy
import numpy as np
M = np.array([[1, 2], [3, 4], [5, 6]])
result = M @ M.T
print(result)
A
[[ 1 3 5]
 [ 2 4 6]]
B
[[ 5 11 17]
 [11 25 39]
 [17 39 61]]
C
[[ 1 2 3]
 [3 4 5]
 [5 6 7]]
D
[[ 5 11 17]
 [11 25 41]
 [17 39 61]]
Attempts:
2 left
💡 Hint
Multiplying a matrix by its transpose results in a symmetric matrix with dot products of rows.
🧠 Conceptual
expert
1:30remaining
Which statement about the @ operator in numpy is TRUE?
Select the correct statement about the @ operator for numpy arrays.
AThe @ operator performs matrix multiplication and requires the inner dimensions to match.
BThe @ operator performs element-wise multiplication of two numpy arrays.
CThe @ operator can multiply any two numpy arrays regardless of their shapes.
DThe @ operator is only used for multiplying numpy arrays with scalars.
Attempts:
2 left
💡 Hint
Think about how matrix multiplication works and what the @ operator does.