Tsallaka zuwa abun ciki
Justice Tech School

Broadcasting

Broadcasting lets arrays of different shapes work together when dimensions match or are 1. A scalar stretches to any shape.

Example
import numpy as np
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m + 10)
print(m + np.array([100, 200, 300]))
print(m * np.array([[1], [10]]))

Result

[[11 12 13]
 [14 15 16]]
[[101 202 303]
 [104 205 306]]
[[ 1  2  3]
 [40 50 60]]

Exercise

Add a scalar to all.

m  10

Quiz Test

When can shapes broadcast?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.