Monday , November 30 2026
NumPy broadcasting rules with examples: scalar, row and column broadcasting, centring data and fixing shape errors.

NumPy Broadcasting Explained with Examples

Updated: 29 November 2026Level: IntermediateReading time: about 8 minutesNumPy 2.x · tested

Broadcasting is how NumPy does arithmetic on arrays of different shapes without copying data or writing loops. It lets you add a number to a whole matrix, subtract each column’s mean, or apply a tax rate per column in one line.

What You Will Learn

  • Scalar and vector broadcasting
  • The broadcasting rules
  • Row vs column broadcasting
  • Centering data by column means
  • Reading broadcasting errors

Prerequisites

Introduction to NumPy.

Examples

Example
import numpy as np
a = np.array([1, 2, 3])
print(a + 10)                      # scalar is stretched to every element

m = np.array([[1, 2, 3],
              [4, 5, 6]])
print(m + a)                       # row (3,) is added to each row of (2, 3)

col = np.array([[10], [20]])
print(m + col)                     # column (2, 1) is added to each column
Output
[11 12 13]
[[2 4 6]
 [5 7 9]]
[[11 12 13]
 [24 25 26]]

The Rules

  1. Compare shapes from the right (last dimension first).
  2. Two dimensions are compatible if they are equal or one of them is 1.
  3. Missing dimensions on the left are treated as 1.
  4. A dimension of size 1 is stretched to match the other.
Shape A Shape B Result
(2, 3) (3,) (2, 3) ✓
(2, 3) (2, 1) (2, 3) ✓
(3, 1) (1, 4) (3, 4) ✓
(2, 3) (2,) Error ✗ – 3 vs 2
Example
print(np.ones((3, 1)).shape, np.ones((1, 4)).shape, (np.ones((3, 1)) + np.ones((1, 4))).shape)
try:
    m + np.array([1, 2])
except ValueError as e:
    print("ValueError:", e)
Output
(3, 1) (1, 4) (3, 4)
ValueError: operands could not be broadcast together with shapes (2,3) (2,)

Explanation: To add [1, 2] to each column of m, reshape it into a column: m + np.array([1, 2])[:, None] (shape (2, 1)).

Real Uses

Example
# centre each column (feature) by subtracting its mean
print((m - m.mean(axis=0)).round(2))

# apply 5% GST to column 1 and 18% to column 2
prices = np.array([[100, 250], [80, 120], [60, 300]])
gst = np.array([0.05, 0.18])
print(prices * (1 + gst))
Output
[[-1.5 -1.5 -1.5]
 [ 1.5  1.5  1.5]]
[[105.  295. ]
 [ 84.  141.6]
 [ 63.  354. ]]

Explanation: m.mean(axis=0) has shape (3,), so it is subtracted from every row – exactly what feature scaling and StandardScaler do. The GST rates broadcast across every product row.

Series

NumPy series: Introduction · Creating Arrays · Data Types · Slicing & Indexing · Array Manipulation · Broadcasting · Math Functions · Statistical Functions · Random · Linear Algebra · NumPy vs Pandas

Interview Questions

Q: What is broadcasting?
NumPy’s rule for operating on arrays of different shapes by virtually stretching size-1 dimensions.

Q: Can (4, 3) and (4,) be added?
No – trailing dimensions 3 and 4 differ; reshape to (4, 1) first.

Practice Exercises

  1. Normalise each column of a (5, 3) array to the 0–1 range using min and max with broadcasting.
  2. Create a 10×10 multiplication table with np.arange(1, 11) and broadcasting.

Summary

Broadcasting compares shapes from the right; equal sizes or 1s are compatible, and size-1 dimensions stretch. It replaces loops for row/column-wise operations.

Next: NumPy Statistical Functions

About Machine Learning

Check Also

Exploratory Data Analysis (EDA) in Python: Step-by-Step Checklist

A step-by-step EDA checklist in pandas with tested outputs: shape, missing values, describe, value counts, groupby, correlations and charts.