Skip to content

Instantly share code, notes, and snippets.

@PaperclipBadger
Created November 18, 2016 22:07
Show Gist options
  • Save PaperclipBadger/7d7632a2d0f0a423529c4d57e5e59c28 to your computer and use it in GitHub Desktop.
Save PaperclipBadger/7d7632a2d0f0a423529c4d57e5e59c28 to your computer and use it in GitHub Desktop.
A demonstration of NumPy's broadcasting semantics.
import numpy as np
a = np.array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
b = np.array([1, 2, 3])
c = np.array([[1],
[2],
[3]])
print(np.array_equal(a + b,
np.array([[2, 3, 4],
[3, 4, 5],
[4, 5, 6]])) # True
print(np.array_equal(a + c,
np.array([[2, 2, 2],
[4, 4, 4],
[6, 6, 6]])) # True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment