Bird
Raised Fist0
NumPydata~20 mins

Broadcasting for outer products in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Broadcasting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of broadcasting outer product with 1D arrays
What is the output of this code snippet using NumPy broadcasting to compute an outer product?
NumPy
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
result = x[:, None] * y
print(result)
A
[[ 4  5]
 [ 8 10]
 [12 15]]
B
[[ 4  8 12]
 [ 5 10 15]]
C
[[ 4  5  6]
 [ 8 10 12]
 [12 15 18]]
D
[[ 5  4]
 [10  8]
 [15 12]]
Attempts:
2 left
💡 Hint
Remember that x[:, None] reshapes x to a column vector to enable broadcasting with y.
❓ data_output
intermediate
1:30remaining
Shape of result from broadcasting outer product
Given two arrays x with shape (4,) and y with shape (3,), what is the shape of the result when computing x[:, None] * y using NumPy broadcasting?
NumPy
import numpy as np
x = np.arange(4)
y = np.arange(3)
result = x[:, None] * y
print(result.shape)
A(4, 3)
B(3, 4)
C(4,)
D(3,)
Attempts:
2 left
💡 Hint
Think about how reshaping x to (4,1) and y to (3,) broadcasts to a 2D array.
🔧 Debug
advanced
1:30remaining
Identify the error in broadcasting outer product code
What error does this code raise when trying to compute an outer product using broadcasting?
NumPy
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
result = x * y
print(result)
AIndexError: index out of bounds
BTypeError: unsupported operand type(s) for *: 'int' and 'list'
CNo error, output is [4 10 15]
DValueError: operands could not be broadcast together with shapes (3,) (2,)
Attempts:
2 left
💡 Hint
Check the shapes of x and y before multiplication.
🚀 Application
advanced
2:00remaining
Use broadcasting to compute outer product without loops
Which code snippet correctly computes the outer product of two 1D NumPy arrays x and y without using loops?
Aresult = x * y[:, None]
Bresult = x[:, None] * y
Cresult = np.dot(x, y)
Dresult = np.outer(x, y) + 1
Attempts:
2 left
💡 Hint
Remember the shape changes needed for broadcasting to work as outer product.
🧠 Conceptual
expert
2:30remaining
Understanding broadcasting rules for outer products
Which statement best explains why x[:, None] * y computes the outer product of 1D arrays x and y in NumPy?
Ax[:, None] transposes x, y is broadcast to match x's shape, resulting in a dot product instead of outer product.
Bx[:, None] flattens x to 1D, y is reshaped to 2D, so broadcasting sums the arrays to produce the outer product.
Cx[:, None] reshapes x to a 2D column vector, y remains 1D, so broadcasting expands y to a row vector, enabling element-wise multiplication to produce a 2D outer product.
Dx[:, None] duplicates x elements, y duplicates y elements, and their multiplication produces a scalar product.
Attempts:
2 left
💡 Hint
Focus on how reshaping changes array dimensions and how broadcasting works with those shapes.

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

  1. 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.
  2. Step 2: Identify what broadcasting enables

    This means you can do element-wise operations without writing loops explicitly.
  3. Final Answer:

    Perform element-wise operations without explicit loops by automatically expanding shapes -> Option B
  4. 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

  1. 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).
  2. 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.
  3. Final Answer:

    a[:, np.newaxis] -> Option C
  4. 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

  1. Step 1: Reshape x for broadcasting

    x[:, np.newaxis] changes x from shape (2,) to (2,1): [[1], [2]].
  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]]
  3. Final Answer:

    [[3 4 5] [6 8 10]] -> Option D
  4. Quick Check:

    Outer product shape (2,3) = [[3 4 5], [6 8 10]] [OK]
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

  1. 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.
  2. Step 2: Identify error cause

    Multiplying arrays of incompatible shapes without reshaping causes a ValueError for shape mismatch.
  3. Final Answer:

    Shape mismatch error because arrays have incompatible shapes for element-wise multiplication -> Option A
  4. 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

  1. 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).
  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).
  3. Final Answer:

    x[:, np.newaxis] * y -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard