Bird
Raised Fist0
NumPydata~10 mins

1D and 2D broadcasting in NumPy - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a 1D array to each row of a 2D array using broadcasting.

NumPy
import numpy as np
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
arr_1d = np.array([10, 20, 30])
result = arr_2d + [1]
print(result)
Drag options to blanks, or click blank then click option'
Aarr_1d
Barr_2d
Cnp.array([1, 2])
Dnp.array([[10], [20], [30]])
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the 2D array to itself instead of the 1D array.
Using a 2D array with incompatible shape for broadcasting.
2fill in blank
medium

Complete the code to multiply a 2D array by a 1D array using broadcasting along columns.

NumPy
import numpy as np
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
arr_1d = np.array([2, 3])
result = arr_2d * [1]
print(result)
Drag options to blanks, or click blank then click option'
Aarr_1d.reshape(3, 1)
Barr_1d.reshape(2, 1)
Carr_1d.reshape(1, 2)
Darr_1d
Attempts:
3 left
💡 Hint
Common Mistakes
Multiplying without reshaping causing shape mismatch error.
Reshaping to wrong dimensions that don't broadcast correctly.
3fill in blank
medium

Complete the code to add a 1D array to each column of a 2D array using broadcasting.

NumPy
import numpy as np
arr_2d = np.array([[1, 2], [3, 4], [5, 6]])
arr_1d = np.array([10, 20, 30])
result = arr_2d + [1]
print(result)
Drag options to blanks, or click blank then click option'
Aarr_1d.reshape(3, 1)
Barr_1d
Carr_1d.reshape(1, 3)
Darr_1d.reshape(3, 2)
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr_1d directly, causing a broadcasting error.
Reshaping to dimensions that don't align properly.
4fill in blank
hard

Fix the error in the code to subtract a 1D array from each column of a 2D array using broadcasting.

NumPy
import numpy as np
arr_2d = np.array([[5, 10, 15], [20, 25, 30]])
arr_1d = np.array([1, 2])
result = arr_2d - [1]
print(result)
Drag options to blanks, or click blank then click option'
Aarr_1d
Barr_1d.reshape(1, 2)
Carr_1d.reshape(3, 1)
Darr_1d.reshape(2, 1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using the 1D array without reshaping causing broadcasting error.
Reshaping to (1, 2) which aligns incorrectly for subtraction.
5fill in blank
hard

Complete the code to compute the outer product of two arrays using broadcasting (no explicit reshape needed).

NumPy
import numpy as np
a = np.array([[1], [2]])
b = np.array([[10, 20, 30]])
result = a * [1]
print(result)
Drag options to blanks, or click blank then click option'
Ab.reshape(3, 1)
Ba
Cb
Dnp.outer(a[:,0], b[0,:])
Attempts:
3 left
💡 Hint
Common Mistakes
Attempting to reshape when not needed.
Confusing the order of multiplication or using np.outer explicitly.

Practice

(1/5)
1. What does broadcasting in NumPy allow you to do with arrays of different shapes?
easy
A. Only add arrays if they have the exact same shape
B. Sort arrays in ascending order
C. Convert arrays into lists automatically
D. Perform element-wise operations without manually reshaping arrays

Solution

  1. Step 1: Understand broadcasting concept

    Broadcasting lets NumPy perform math on arrays with different shapes by stretching smaller arrays.
  2. Step 2: Identify what broadcasting enables

    This means you can do element-wise operations without manually reshaping arrays to the same shape.
  3. Final Answer:

    Perform element-wise operations without manually reshaping arrays -> Option D
  4. Quick Check:

    Broadcasting = element-wise operations on different shapes [OK]
Hint: Broadcasting stretches smaller arrays to match bigger ones [OK]
Common Mistakes:
  • Thinking arrays must have the same shape to operate
  • Confusing broadcasting with sorting or reshaping
  • Assuming broadcasting copies data
2. Which of the following is the correct way to add a 1D array b = np.array([1, 2, 3]) to each row of a 2D array a with shape (4, 3)?
easy
A. a + b
B. a + b.T
C. a + b.reshape(3, 1)
D. a + b.reshape(1, 4)

Solution

  1. Step 1: Check shapes of arrays

    Array a has shape (4, 3), and b has shape (3,). Adding a + b broadcasts b across rows.
  2. Step 2: Understand broadcasting rules

    Since b matches the number of columns, it adds to each row correctly without reshaping.
  3. Final Answer:

    a + b -> Option A
  4. Quick Check:

    1D array added across rows = a + b [OK]
Hint: Add 1D array directly if its length matches columns [OK]
Common Mistakes:
  • Trying to transpose 1D array which has no effect
  • Reshaping to wrong dimensions causing errors
  • Confusing rows and columns in shapes
3. What is the output of the following code?
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([10, 20, 30])
result = a + b
print(result)
medium
A. [[11 22 33] [14 25 36]]
B. [[11 12 13] [14 15 16]]
C. [[10 20 30] [10 20 30]]
D. Error due to shape mismatch

Solution

  1. Step 1: Analyze shapes and operation

    a shape is (2,3), b shape is (3,). Adding broadcasts b across rows.
  2. Step 2: Calculate element-wise addition

    Row 1: [1+10, 2+20, 3+30] = [11, 22, 33]
    Row 2: [4+10, 5+20, 6+30] = [14, 25, 36]
  3. Final Answer:

    [[11 22 33] [14 25 36]] -> Option A
  4. Quick Check:

    Broadcast add across rows = [[11 22 33], [14 25 36]] [OK]
Hint: Add 1D array to 2D rows matches columns [OK]
Common Mistakes:
  • Expecting error due to different shapes
  • Adding element-wise without broadcasting
  • Confusing rows and columns in addition
4. Identify the error in this code snippet:
import numpy as np
a = np.array([[1, 2], [3, 4], [5, 6]])
b = np.array([10, 20, 30])
result = a + b
print(result)
medium
A. Using wrong operator for addition
B. Syntax error in array creation
C. Shapes of a and b are incompatible for broadcasting
D. No error, code runs fine

Solution

  1. Step 1: Check shapes of arrays

    a shape is (3, 2), b shape is (3,). They differ in second dimension.
  2. Step 2: Understand broadcasting rules

    Broadcasting requires trailing dimensions to match or be 1. Here, 2 != 3, so addition fails.
  3. Final Answer:

    Shapes of a and b are incompatible for broadcasting -> Option C
  4. Quick Check:

    Shape mismatch causes broadcasting error [OK]
Hint: Check trailing dimensions match or are 1 for broadcasting [OK]
Common Mistakes:
  • Assuming any shapes can broadcast
  • Ignoring dimension order in arrays
  • Thinking 1D arrays always broadcast to 2D
5. You have a 2D array data of shape (5, 4) representing 5 samples with 4 features each. You want to subtract the mean of each feature (given as a 1D array mean_vals of length 4) from the corresponding feature column in data. Which code correctly applies broadcasting to do this?
hard
A. data - mean_vals.reshape(4, 1)
B. data - mean_vals
C. data - mean_vals.reshape(1, 5)
D. data - mean_vals.T

Solution

  1. Step 1: Understand shapes and operation

    data shape is (5, 4), mean_vals shape is (4,). Subtracting mean_vals from data broadcasts mean_vals across rows.
  2. Step 2: Confirm correct broadcasting

    Since mean_vals length matches columns, data - mean_vals subtracts feature-wise means correctly.
  3. Final Answer:

    data - mean_vals -> Option B
  4. Quick Check:

    Subtract 1D array from 2D rows = data - mean_vals [OK]
Hint: Subtract 1D array matching columns directly [OK]
Common Mistakes:
  • Reshaping mean_vals incorrectly causing errors
  • Transposing 1D array which has no effect
  • Trying to subtract mean_vals across wrong axis
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