Skip to main content
Editor

NumPy Broadcasting: Operate on Arrays of Different Shapes

Advanced25 min5 exercises100 XP
0/5 exercises

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.

Broadcasting is NumPy's set of rules for operating on arrays with different shapes. It lets you add a single row to every row of a matrix, or subtract column means without writing a single loop. Once you understand it, you'll write cleaner and faster code.

In this tutorial, you'll learn the three broadcasting rules, how shape compatibility works, practical patterns for centering and scaling data, and how to debug shape mismatches.

What Is Broadcasting?

Broadcasting is how NumPy handles operations between arrays of different shapes. Instead of requiring you to manually duplicate data to make shapes match, NumPy "stretches" the smaller array virtually - without actually copying memory.

basic_broadcast.py
import numpy as np
# Scalar broadcast: scalar is stretched to match the array
a = np.array([10, 20, 30])
result = a + 5
print(result) # [15 25 35]
# 1D + 2D broadcast: row is stretched across all rows
matrix = np.array([[1, 2, 3],
[4, 5, 6]])
row = np.array([10, 20, 30])
print(matrix + row)
# [[11 22 33]
# [14 25 36]]
Output
[15 25 35]
[[11 22 33]
 [14 25 36]]

In the second example, the row [10, 20, 30] has shape (3,) and the matrix has shape (2, 3). NumPy conceptually repeats the row to create a (2, 3) array, then adds element-wise. No actual memory duplication happens - it's just smart indexing.

What Are the Three Broadcasting Rules?

NumPy follows three rules (checked in order) to determine if two shapes are compatible for broadcasting:

Rule 1: If the arrays have different numbers of dimensions, the shape of the smaller array is padded with 1s on the left side.

Rule 2: Arrays with a size of 1 along a particular dimension act as if they had the size of the larger array in that dimension.

Rule 3: If shapes disagree along any dimension and neither is 1, broadcasting fails with an error.

rules_example.py
import numpy as np
# Example: (2, 3) + (3,)
# Step 1: Pad (3,) to (1, 3)
# Step 2: Stretch (1, 3) to (2, 3)
# Result shape: (2, 3) -- compatible!
a = np.ones((2, 3))
b = np.array([10, 20, 30]) # shape (3,)
print((a + b).shape) # (2, 3)
print(a + b)
# [[11. 21. 31.]
# [11. 21. 31.]]
Output
(2, 3)
[[11. 21. 31.]
 [11. 21. 31.]]

Let's trace through a more complex example. A shape (4, 1) array and a shape (3,) array:

outer_addition.py
import numpy as np
# Shape (4, 1) + shape (3,)
# Step 1: Pad (3,) to (1, 3)
# Step 2: Stretch (4, 1) to (4, 3) and (1, 3) to (4, 3)
# Result: (4, 3)
col = np.array([[1], [2], [3], [4]]) # shape (4, 1)
row = np.array([10, 20, 30]) # shape (3,)
print(col + row)
# [[11 21 31]
# [12 22 32]
# [13 23 33]
# [14 24 34]]
Output
[[11 21 31]
 [12 22 32]
 [13 23 33]
 [14 24 34]]

When Does Broadcasting Fail?

Broadcasting fails when two dimensions disagree and neither is 1. For example, shapes (3,) and (4,) are incompatible - 3 and 4 are different and neither is 1.

broadcast_fail.py
import numpy as np
a = np.array([1, 2, 3]) # shape (3,)
b = np.array([10, 20, 30, 40]) # shape (4,)
# This would fail:
# print(a + b) # ValueError: shapes (3,) and (4,) not aligned
# But this works - reshape one to (3, 1):
a_col = a.reshape(3, 1) # shape (3, 1)
print(a_col + b) # shape (3, 4)
# [[11 21 31 41]
# [12 22 32 42]
# [13 23 33 43]]
Output
[[11 21 31 41]
 [12 22 32 42]
 [13 23 33 43]]

How Do You Use Broadcasting in Practice?

The two most common real-world uses of broadcasting are centering data (subtracting the mean of each column) and scaling data (dividing by the standard deviation). These are fundamental steps in data preprocessing.

centering.py
import numpy as np
# 4 students, 3 subjects
scores = np.array([[85, 70, 92],
[90, 65, 88],
[78, 80, 95],
[92, 75, 84]])
# Center each column: subtract column means
col_means = np.mean(scores, axis=0) # shape (3,)
print('Column means:', col_means)
centered = scores - col_means # (4,3) - (3,) broadcasts!
print('Centered:\n', centered)
# Scale: divide by column std
col_std = np.std(scores, axis=0)
standardized = centered / col_std
print('Standardized:\n', np.round(standardized, 2))
Output
Column means: [86.25 72.5  89.75]
Centered:
 [[-1.25 -2.5   2.25]
 [ 3.75 -7.5  -1.75]
 [-8.25  7.5   5.25]
 [ 5.75  2.5  -5.75]]
Standardized:
 [[-0.23 -0.45  0.54]
 [ 0.69 -1.34 -0.42]
 [-1.53  1.34  1.27]
 [ 1.06  0.45 -1.39]]

Notice how scores - col_means works without any loops. The column means array has shape (3,), which broadcasts against (4, 3) by stretching across all 4 rows. This is the standard way to z-score normalize data in machine learning.

newaxis.py
import numpy as np
# Normalize each ROW (subtract row mean)
scores = np.array([[85, 70, 92],
[90, 65, 88],
[78, 80, 95]])
row_means = np.mean(scores, axis=1) # shape (3,)
print('Row means:', row_means)
# Need (3, 1) to broadcast across columns
row_means_col = row_means[:, np.newaxis] # shape (3, 1)
centered = scores - row_means_col # (3, 3) - (3, 1) works!
print(np.round(centered, 2))
Output
Row means: [82.33333333 81.         84.33333333]
[[  2.67 -12.33   9.67]
 [  9.   -16.     7.  ]
 [ -6.33  -4.33  10.67]]

Quick Reference: Shape Compatibility

Here are common broadcasting patterns you'll encounter:

patterns.py
import numpy as np
# Pattern 1: Scalar + Array -- () + (n,) -> (n,)
print(np.array([1, 2, 3]) + 10) # [11 12 13]
# Pattern 2: Row + Matrix -- (n,) + (m, n) -> (m, n)
print(np.ones((2, 3)) + np.array([1, 2, 3]))
# Pattern 3: Column + Row -- (m, 1) + (n,) -> (m, n)
col = np.array([[1], [2]])
row = np.array([10, 20, 30])
print(col * row) # [[10 20 30], [20 40 60]]
# Pattern 4: FAILS -- (3,) + (4,)
# np.array([1,2,3]) + np.array([1,2,3,4]) # ValueError!
Output
[11 12 13]
[[2. 3. 4.]
 [2. 3. 4.]]
[[10 20 30]
 [20 40 60]]

Practice Exercises

Convert Temperatures
Write Code

Given an array of temperatures in Celsius celsius = np.array([0, 20, 37, 100]), convert them all to Fahrenheit using the formula F = C * 9/5 + 32. Print the result.

import numpy as np
celsius = np.array([0, 20, 37, 100])
# Convert to Fahrenheit and print
Add Bonus Points
Write Code

A teacher has a 3x4 grades matrix and wants to add different bonus points to each exam (column). The bonus array is [5, 3, 8, 2]. Use broadcasting to add the bonus to all students at once. Print the result.

grades = np.array([[70, 85, 60, 90],
                   [80, 75, 85, 70],
                   [90, 95, 70, 80]])
bonus = np.array([5, 3, 8, 2])
import numpy as np
grades = np.array([[70, 85, 60, 90],
[80, 75, 85, 70],
[90, 95, 70, 80]])
bonus = np.array([5, 3, 8, 2])
# Add bonus to grades and print
Center Each Row
Write Code

Given the matrix below, subtract each row's mean from that row (center each row). You'll need np.newaxis or .reshape(-1, 1) to make the shapes compatible. Print the centered matrix.

data = np.array([[10, 20, 30],
                 [40, 50, 60],
                 [70, 80, 90]])
import numpy as np
data = np.array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
# Subtract each row's mean from that row
Predict the Broadcast Shape
Predict Output

What 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)
Multiplication Table
Write Code

Use broadcasting to create a 5x5 multiplication table for numbers 1 through 5. The result should be a 5x5 array where element [i, j] equals (i+1) * (j+1). Print it.

Hint: create a column vector and a row vector, then multiply them.

import numpy as np
# Create a 5x5 multiplication table

Harder problems

More problems on this topic: Grids and matrices

Related Tutorials

Was this tutorial helpful?