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.
Practice
(1/5)
1. What does broadcasting allow you to do in NumPy when working with arrays of different shapes?
easy
A. Convert arrays into lists for easier manipulation
B. Perform element-wise operations without explicit loops by automatically expanding shapes
C. Sort arrays in ascending order automatically
D. Create new arrays filled with zeros matching the shape of another array
Solution
Step 1: Understand broadcasting concept
Broadcasting lets NumPy perform operations on arrays of different shapes by expanding the smaller array's shape without copying data.
Step 2: Identify what broadcasting enables
This means you can do element-wise operations without writing loops explicitly.
Final Answer:
Perform element-wise operations without explicit loops by automatically expanding shapes -> Option B
Quick Check:
Broadcasting = element-wise operations without loops [OK]
Hint: Broadcasting expands shapes to match for element-wise ops [OK]
Common Mistakes:
Thinking broadcasting converts arrays to lists
Confusing broadcasting with sorting
Assuming broadcasting creates new zero arrays
2. Which of the following is the correct way to reshape a 1D NumPy array a to a column vector for broadcasting?
easy
A. a[np.newaxis, :]
B. a.reshape(1, -1)
C. a[:, np.newaxis]
D. a.flatten()
Solution
Step 1: Understand reshaping to column vector
A column vector has shape (n, 1). Using np.newaxis adds a new axis. a[:, np.newaxis] converts shape (n,) to (n,1).
Step 2: Check other options
a.reshape(1, -1) makes a row vector (1, n). a[np.newaxis, :] also makes a row vector. a.flatten() flattens array to 1D, no new axis.
Final Answer:
a[:, np.newaxis] -> Option C
Quick Check:
Column vector = a[:, np.newaxis] [OK]
Hint: Use a[:, np.newaxis] to add a column axis [OK]
Common Mistakes:
Using reshape(1, -1) creates a row, not column
Confusing row and column vector shapes
Using flatten removes dimensions instead of adding
3. What is the output of the following code?
import numpy as np
x = np.array([1, 2])
y = np.array([3, 4, 5])
result = x[:, np.newaxis] * y
print(result)
medium
A. [[3 6]
[4 8]
[5 10]]
B. [[3 4]
[5 6]]
C. [3 4 5 6 8 10]
D. [[3 4 5]
[6 8 10]]
Solution
Step 1: Reshape x for broadcasting
x[:, np.newaxis] changes x from shape (2,) to (2,1): [[1], [2]].
Step 2: Multiply with y
y has shape (3,), broadcasted to (1,3): [3,4,5]. Multiplying (2,1) * (1,3) results in (2,3) array: [[1*3, 1*4, 1*5], [2*3, 2*4, 2*5]] = [[3,4,5],[6,8,10]]
Hint: Reshape first array to (n,1) to multiply outer product [OK]
Common Mistakes:
Not reshaping x, causing shape mismatch
Confusing row and column broadcasting
Expecting 1D output instead of 2D
4. The following code is intended to compute the outer product of two arrays a and b. What is the error?
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5])
result = a * b
print(result)
medium
A. Shape mismatch error because arrays have incompatible shapes for element-wise multiplication
B. Syntax error due to missing parentheses
C. Result is a scalar instead of an array
D. No error; code runs correctly
Solution
Step 1: Check shapes of a and b
a has shape (3,), b has shape (2,). They cannot be multiplied element-wise directly because shapes differ and are not broadcast-compatible.
Step 2: Identify error cause
Multiplying arrays of incompatible shapes without reshaping causes a ValueError for shape mismatch.
Final Answer:
Shape mismatch error because arrays have incompatible shapes for element-wise multiplication -> Option A
Quick Check:
Shape mismatch error = incompatible shapes [OK]
Hint: Check shapes before multiplying; reshape if needed [OK]
Common Mistakes:
Assuming arrays multiply element-wise without matching shapes
Thinking code has syntax errors
Expecting scalar output from array multiplication
5. You want to compute the outer product of two arrays x = np.array([1, 2, 3]) and y = np.array([4, 5]) using broadcasting without explicit loops. Which code snippet correctly does this and produces a (3, 2) shaped result?
hard
A. x[:, np.newaxis] * y
B. np.dot(x, y)
C. x * y[:, np.newaxis]
D. np.multiply.outer(x, y)
Solution
Step 1: Understand shapes and broadcasting
x has shape (3,), y has shape (2,). To get outer product shape (3,2), reshape x to (3,1) and multiply by y (2,) broadcasted to (1,2).
Step 2: Evaluate options
np.multiply.outer(x, y) does exist as a function but returns a (3,2) array; however, the question asks for broadcasting without explicit loops and np.multiply.outer is a function call, not broadcasting syntax. np.dot(x, y) computes dot product, resulting in scalar. x * y[:, np.newaxis] reshapes y to (2,1), resulting in (3,) * (2,1) which broadcasts to (2,3) (swapped dimensions), not (3,2). x[:, np.newaxis] * y reshapes x to (3,1) and multiplies by y, producing (3,2).
Final Answer:
x[:, np.newaxis] * y -> Option A
Quick Check:
Broadcast x to (3,1) and multiply y (2,) = (3,2) [OK]
Hint: Reshape first array to (n,1) to get outer product shape [OK]
Common Mistakes:
Using np.dot instead of element-wise multiplication
Confusing np.multiply.outer with broadcasting syntax
Swapping axes by reshaping wrong array
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.