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
Matrix multiplication with @ operator
📖 Scenario: Imagine you work in a small company that analyzes sales data. You have two tables of numbers: one shows sales by product and month, and the other shows the price of each product. You want to find the total sales revenue for each month by multiplying these tables.
🎯 Goal: You will create two matrices using numpy, then multiply them using the @ operator to find total sales revenue per month.
📋 What You'll Learn
Create two numpy arrays with exact values
Use a variable to store the result of matrix multiplication
Use the @ operator for matrix multiplication
Print the resulting matrix
💡 Why This Matters
🌍 Real World
Matrix multiplication is used in many fields like sales analysis, computer graphics, and machine learning to combine data efficiently.
💼 Career
Knowing how to multiply matrices with numpy is a key skill for data scientists and analysts working with numerical data.
Progress0 / 4 steps
1
Create sales and price matrices
Import numpy as np. Create a numpy array called sales with these exact values: [[10, 20, 30], [5, 10, 25]]. Create another numpy array called prices with these exact values: [[2], [3], [4]].
NumPy
Hint
Use np.array() to create matrices with the exact numbers given.
2
Create a variable for total revenue
Create a variable called total_revenue and set it to None as a placeholder.
NumPy
Hint
Just create the variable total_revenue and assign None for now.
3
Multiply matrices using @ operator
Use the @ operator to multiply sales by prices. Store the result in the variable total_revenue.
NumPy
Hint
Use total_revenue = sales @ prices to multiply the matrices.
4
Print the total revenue matrix
Print the variable total_revenue to display the result of the matrix multiplication.
NumPy
Hint
Use print(total_revenue) to show the final matrix.
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