0
0
NumPydata~5 mins

Matrix multiplication with @ operator in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
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)
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
Which numpy function is equivalent to using the @ operator?
Anp.dot()
Bnp.multiply()
Cnp.add()
Dnp.cross()
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
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.