Predict the Broadcast Shape
Advanced · Predict the output · from NumPy Broadcasting: Operate on Arrays of Different Shapes
From the lesson: NumPy Broadcasting: Operate on Arrays of Different Shapes
You've already seen that np.array([1,2,3]) 2 multiplies every element by 2. But have you wondered how* NumPy knows to stretch that single number 2 across the entire array? That mechanism is called broadcasting.
Read the lessonWhat does the following code print? Trace through the broadcasting rules carefully.
import numpy as np
a = np.ones((4, 1))
b = np.ones((1, 3))
c = a + b
print(c.shape)
print(c)import numpy as np
a = np.ones((4, 1))
b = np.ones((1, 3))
c = a + b
print(c.shape)
print(c)
More NumPy Broadcasting exercises
- Convert TemperaturesAdvanced
- Add Bonus PointsAdvanced
- Center Each RowAdvanced
- Multiplication TableAdvanced
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz