We reshape vectors to align dimensions, then numpy broadcasts them to multiply element-wise, producing the outer product matrix.
Execution Sample
NumPy
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
outer = x[:, None] * y
print(outer)
This code computes the outer product of two 1D arrays x and y using broadcasting.
Execution Table
Step
Variable
Shape
Operation
Result/Value
1
x
(3,)
Initial array
[1 2 3]
2
y
(2,)
Initial array
[4 5]
3
x[:, None]
(3,1)
Reshape x to column vector
[[1]
[2]
[3]]
4
y
(2,)
Reshape y to row vector (broadcasted as is)
[4 5]
5
Broadcast shapes
(3,1) and (2,) -> (3,2)
Broadcast x[:, None] and y
Shapes compatible for broadcasting
6
outer
(3,2)
Element-wise multiply
[[ 4 5]
[ 8 10]
[12 15]]
7
-
-
Print outer
[[ 4 5]
[ 8 10]
[12 15]]
💡 Completed outer product calculation with broadcasting.
Variable Tracker
Variable
Start
After Step 3
After Step 6
Final
x
[1 2 3]
[1 2 3]
[1 2 3]
[1 2 3]
y
[4 5]
[4 5]
[4 5]
[4 5]
outer
-
-
[[ 4 5]
[ 8 10]
[12 15]]
[[ 4 5]
[ 8 10]
[12 15]]
Key Moments - 3 Insights
Why do we use x[:, None] instead of just x?
Using x[:, None] reshapes x from shape (3,) to (3,1), making it a column vector. This allows broadcasting with y (shape (2,)) to produce a 2D outer product. Without reshaping, shapes are not compatible for broadcasting as intended (see execution_table step 3).
How does broadcasting work between shapes (3,1) and (2,)?
Numpy compares shapes from right to left: (3,1) and (2,) become (3,1) and (1,2) by prepending 1. Then dimensions 1 and 2 broadcast to 2, and 3 and 1 broadcast to 3, resulting in (3,2) shape (see execution_table step 5).
What is the shape of the final outer product?
The final outer product has shape (3,2), combining the length of x (3) and y (2) after broadcasting and multiplication (see execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3. What is the shape of x after reshaping?
A(1,3)
B(3,1)
C(3,)
D(2,)
💡 Hint
Check the 'Shape' column at step 3 in the execution_table.
At which step does numpy broadcast the arrays to compatible shapes?
AStep 5
BStep 6
CStep 4
DStep 7
💡 Hint
Look for the step mentioning 'Broadcast shapes' in the execution_table.
If y was reshaped to (1,2) explicitly, how would the broadcasting step change?
ABroadcasting would fail due to shape mismatch
BResult shape would become (2,3)
CBroadcasting would still produce shape (3,2)
DResult would be a 1D array
💡 Hint
Broadcasting works by aligning trailing dimensions; reshaping y to (1,2) matches x[:, None] shape (3,1) to produce (3,2).
Concept Snapshot
Broadcasting for outer products:
- Reshape x to (n,1) using x[:, None]
- y remains (m,) or reshape to (1,m)
- Numpy broadcasts shapes (n,1) and (m,) to (n,m)
- Element-wise multiply to get outer product matrix
- Result shape is (len(x), len(y))
Full Transcript
This visual execution shows how numpy computes the outer product of two 1D arrays using broadcasting. First, the array x is reshaped from (3,) to (3,1) to become a column vector. The array y remains (2,) as a row vector. Numpy broadcasts these shapes to (3,2) by aligning dimensions. Then element-wise multiplication produces the outer product matrix. The final result is a 2D array with shape (3,2). Key moments include why reshaping x is necessary, how broadcasting aligns shapes, and the shape of the final output.