📖 Scenario: Imagine you are working with two sets of numbers representing sales data from two different stores. You want to check if these two sets can be combined using NumPy broadcasting rules to perform element-wise operations easily.
🎯 Goal: You will create two NumPy arrays, set a configuration variable to select one array's shape, check if the two arrays are compatible for broadcasting, and finally print the result.
📋 What You'll Learn
Create two NumPy arrays with exact shapes and values
Create a configuration variable to select which array's shape to check
Use NumPy's broadcasting rules to check compatibility
Print the compatibility result as True or False
💡 Why This Matters
🌍 Real World
Broadcasting is used in data science to perform operations on datasets of different shapes without writing loops, making calculations faster and code simpler.
💼 Career
Understanding broadcasting helps data scientists and analysts efficiently manipulate and combine data arrays, which is essential for data cleaning, feature engineering, and model input preparation.
Progress0 / 4 steps
1
Create two NumPy arrays
Import NumPy as np. Create a NumPy array called store1 with values [[10, 20, 30], [40, 50, 60]] and another array called store2 with values [5, 15, 25].
NumPy
Hint
Use np.array() to create arrays with the exact values and shapes.
2
Set configuration variable
Create a variable called check_shape and set it to the shape of store1.
NumPy
Hint
Use the .shape attribute of the array store1 to get its shape.
3
Check broadcasting compatibility
Use np.broadcast_shapes() with the shapes of store1 and store2 inside a try block to check if they can broadcast. Create a variable called compatible and set it to True if broadcasting is possible, otherwise set it to False in the except block.
NumPy
Hint
Use a try-except block to catch ValueError from np.broadcast_shapes().
4
Print the compatibility result
Print the value of the variable compatible.
NumPy
Hint
Use print(compatible) to show the result.
Practice
(1/5)
1. Which of the following pairs of shapes are compatible for broadcasting in NumPy?
easy
A. (1, 2, 3) and (2, 3, 1)
B. (2, 3) and (3, 2)
C. (4, 5) and (4, 5, 1)
D. (3, 1) and (1, 4)
Solution
Step 1: Understand broadcasting rules
Broadcasting compares shapes from right to left. Dimensions must be equal or one must be 1.
Step 2: Check each pair
For (3,1) and (1,4), rightmost dims 1 and 4 are compatible (1 can broadcast to 4), left dims 3 and 1 compatible (1 can broadcast to 3). So compatible.
Final Answer:
(3, 1) and (1, 4) are compatible -> Option D
Quick Check:
Broadcast dims right to left, 1 can expand [OK]
Hint: Check shapes from right; 1 or equal dims broadcast [OK]
Common Mistakes:
Assuming all different shapes are compatible
Not comparing dimensions from right to left
Ignoring that 1 can broadcast to any size
2. Which of the following is the correct way to check broadcasting compatibility of shapes (2, 3) and (3,) using NumPy?
easy
A. numpy.broadcast_shapes((2, 3), (3,))
B. numpy.broadcast((2, 3), (3,))
C. numpy.broadcast_shapes([2, 3], 3)
D. numpy.broadcast((2, 3), (3))
Solution
Step 1: Identify the correct function
NumPy provides numpy.broadcast_shapes to check shape compatibility, which takes tuples as shape arguments.
Step 2: Check argument types
numpy.broadcast_shapes((2, 3), (3,)) uses numpy.broadcast_shapes with tuples, which is correct. numpy.broadcast_shapes([2, 3], 3) uses a scalar for the second argument, which is invalid for this function.
Final Answer:
numpy.broadcast_shapes((2, 3), (3,)) -> Option A
Quick Check:
Use numpy.broadcast_shapes with tuples [OK]
Hint: Use numpy.broadcast_shapes with tuples for shape check [OK]
Common Mistakes:
Using numpy.broadcast instead of numpy.broadcast_shapes