0
0
NumPydata~5 mins

Broadcasting for distance matrices in NumPy

Choose your learning style9 modes available
Introduction

Broadcasting helps us calculate distances between many points quickly without writing loops.

You want to find distances between all pairs of cities on a map.
You need to compare many customers' locations to stores to find the closest one.
You want to measure how similar or different many items are based on their features.
You want to speed up calculations by avoiding slow loops in Python.
Syntax
NumPy
distances = np.sqrt(((points1[:, None, :] - points2[None, :, :]) ** 2).sum(axis=2))

points1 and points2 are arrays of points with shape (n, d) and (m, d).

Broadcasting automatically expands dimensions to subtract every point in points1 from every point in points2.

Examples
Calculate distances between each pair of points in the same array.
NumPy
import numpy as np
points = np.array([[1, 2], [4, 6], [7, 8]])
dist_matrix = np.sqrt(((points[:, None, :] - points[None, :, :]) ** 2).sum(axis=2))
Calculate distances between points in A and points in B.
NumPy
import numpy as np
A = np.array([[0, 0], [1, 1]])
B = np.array([[1, 0], [2, 2], [3, 3]])
distances = np.sqrt(((A[:, None, :] - B[None, :, :]) ** 2).sum(axis=2))
Sample Program

This program calculates the distance between each point in points_A and each point in points_B using broadcasting. The result is a 2x3 matrix where each element is the distance between a point from A and a point from B.

NumPy
import numpy as np

# Define two sets of points
points_A = np.array([[0, 0], [3, 4]])  # Two points
points_B = np.array([[0, 4], [3, 0], [6, 8]])  # Three points

# Calculate the distance matrix using broadcasting
# points_A[:, None, :] shape: (2,1,2)
# points_B[None, :, :] shape: (1,3,2)
dist_matrix = np.sqrt(((points_A[:, None, :] - points_B[None, :, :]) ** 2).sum(axis=2))

print("Distance matrix:")
print(dist_matrix)
OutputSuccess
Important Notes

Broadcasting lets numpy do math on arrays with different shapes by expanding them automatically.

Make sure the last dimension matches the number of coordinates (e.g., 2 for 2D points).

This method is much faster than using loops for large sets of points.

Summary

Broadcasting helps calculate distances between many points without loops.

Use shape tricks like adding new axes to subtract all pairs at once.

It works well for 2D, 3D, or higher-dimensional points.