📖 Scenario: Imagine you are working with sales data for a small store. You have the sales numbers for one product over a week, but you want to compare it with a target sales array that has the same shape as the weekly data for easy calculations.
🎯 Goal: You will create a numpy array for weekly sales, then use np.broadcast_to() to expand a single target sales value to match the weekly sales shape. Finally, you will print the broadcasted target sales array.
📋 What You'll Learn
Create a numpy array called weekly_sales with the exact values: [10, 15, 20, 25, 30, 35, 40]
Create a variable called target_sales with the exact value 20
Use np.broadcast_to() to create a new array called broadcasted_target that has the same shape as weekly_sales but with all values equal to target_sales
Print the broadcasted_target array
💡 Why This Matters
🌍 Real World
Broadcasting helps when you want to compare or combine data of different shapes without manually repeating values. For example, comparing daily sales to a fixed target.
💼 Career
Data scientists often use broadcasting to efficiently perform operations on arrays of different shapes, saving time and memory.
Progress0 / 4 steps
1
Create the weekly sales data array
Create a numpy array called weekly_sales with these exact values: [10, 15, 20, 25, 30, 35, 40]
NumPy
Hint
Use np.array() to create the array with the exact list of numbers.
2
Set the target sales value
Create a variable called target_sales and set it to the integer value 20
NumPy
Hint
Just assign the number 20 to the variable target_sales.
3
Broadcast the target sales value to match weekly sales shape
Use np.broadcast_to() to create a new array called broadcasted_target that has the same shape as weekly_sales but with all values equal to target_sales
NumPy
Hint
Use np.broadcast_to(target_sales, weekly_sales.shape) to expand the single value to the shape of weekly_sales.
4
Print the broadcasted target sales array
Print the broadcasted_target array to see the result
NumPy
Hint
Use print(broadcasted_target) to display the array.
Practice
(1/5)
1. What does np.broadcast_to() do in NumPy?
easy
A. It sorts the elements of an array in ascending order.
B. It removes duplicate elements from an array.
C. It stretches an array to a larger shape without copying data.
D. It converts an array into a list.
Solution
Step 1: Understand the purpose of np.broadcast_to()
This function is used to stretch an array to a bigger shape so it can be used in element-wise operations with other arrays.
Step 2: Compare with other options
Sorting, removing duplicates, or converting to list are unrelated to broadcasting.
Final Answer:
It stretches an array to a larger shape without copying data. -> Option C
Quick Check:
Broadcasting = Stretch array shape [OK]
Hint: Broadcasting means stretching shape, not sorting or converting [OK]
Common Mistakes:
Confusing broadcasting with sorting or reshaping
Thinking it copies data instead of reusing
Mixing it up with array conversion functions
2. Which of the following is the correct syntax to broadcast array a to shape (3, 4)?
easy
A. np.broadcast_to(a, (3, 4))
B. np.broadcast(a, shape=(3, 4))
C. np.broadcast_to(a, shape=3, 4)
D. np.broadcast(a, (3, 4))
Solution
Step 1: Recall the function signature
The correct function is np.broadcast_to(array, shape) where shape is a tuple.
Step 2: Check each option
np.broadcast_to(a, (3, 4)) matches the correct syntax. np.broadcast_to(a, shape=3, 4) incorrectly uses shape=3, 4 without tuple (int shape invalid). Options B and D use a non-existent np.broadcast function.
Thinking broadcasting repeats columns instead of rows
Expecting a flattened array
Assuming an error due to shape mismatch
4. What is wrong with this code?
import numpy as np
arr = np.array([1, 2])
result = np.broadcast_to(arr, (3, 3))
print(result)
medium
A. ValueError because shape (3, 3) is incompatible with input shape (2,).
B. SyntaxError due to missing parentheses.
C. TypeError because broadcast_to expects a list, not an array.
D. No error; output is a 3x3 array with repeated rows.
Solution
Step 1: Check input and target shapes
The input array has shape (2,), but the target shape is (3, 3). Broadcasting requires the input shape to be compatible with the target shape.
Step 2: Understand broadcasting rules
Since 2 does not match 3 in the last dimension, broadcasting cannot stretch (2,) to (3, 3). This causes a ValueError.
Final Answer:
ValueError because shape (3, 3) is incompatible with input shape (2,). -> Option A
Quick Check:
Shape mismatch causes ValueError [OK]
Hint: Broadcast shapes must be compatible; check dimensions carefully [OK]
Common Mistakes:
Assuming broadcasting always works regardless of shape
Confusing ValueError with TypeError
Ignoring dimension mismatch
5. You have a 1D array a = np.array([5]) and want to create a 3x4 array where every element is 5 using np.broadcast_to(). Which code correctly achieves this?
hard
A. np.broadcast_to(a, (4, 3))
B. np.broadcast_to(a, (3, 4))
C. np.broadcast_to(a, (3,))
D. np.broadcast_to(a, (5, 5))
Solution
Step 1: Identify the input and desired output shape
The input is a 1D array with shape (1,), and the desired output shape is (3, 4).
Step 2: Apply broadcasting rules
Broadcasting can stretch (1,) to (3, 4) by repeating the single value 5 across all elements.
Step 3: Check options
np.broadcast_to(a, (3, 4)) correctly uses np.broadcast_to(a, (3, 4)). np.broadcast_to(a, (4, 3)) swaps dimensions, which is not the requested shape. np.broadcast_to(a, (3,)) is only 1D, and np.broadcast_to(a, (5, 5)) has wrong shape.
Final Answer:
np.broadcast_to(a, (3, 4)) -> Option B
Quick Check:
Broadcast single value to (3,4) with np.broadcast_to [OK]
Hint: Broadcast single-element array to target shape tuple [OK]
Common Mistakes:
Mixing up shape dimensions order
Using wrong target shape tuple
Trying to broadcast to incompatible shapes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.