attempt to explain why broadcasting is bae
A starter example is centering a matrix | |
```{python} | |
import numpy as np | |
X = np.random.normal(size=(3, 3)) # random 3 by 3 matrix | |
mu = X.mean(axis = 0) # array (vector) of column means | |
print("Original matrix") | |
print(X) | |
print("\nArray of column means") | |
print(mu) | |
# array effectively copied till vector matches matrix in size | |
print("\nBroadcast subtraction operation") | |
print(X - mu) | |
``` | |
To do this in R you have to loop or use something like map/apply. | |
This lets you create kernel matrices in a fast and fairly readable way, for example. | |
```{python} | |
import numpy as np | |
X = np.random.normal(size=(10, 5)) | |
Y = np.random.normal(size=(2, 5)) | |
sigma = 0.1 | |
# calculate all pairwise distances between rows in X and Y | |
# (x - y)^2 = a^2 - 2 ab + b^2 | |
dist_sq = np.square(X).sum(axis=1)[:, np.newaxis] - 2.0 * np.dot(X, Y.T) + np.square(Y).sum(axis=1) | |
# turn this into an RBF kernel (math might be off, but hopefully the point is clear) | |
rbf = np.exp(-np.sqrt(dist_sq) / (2 * sigma ** 2)) | |
print(dist_sq) | |
print(rbf) | |
``` | |
In R you have to hope someone vectorized the components of the operation that | |
you care about, copy an object along an appropriate dimension, or use | |
loops/applys/maps, which I think make the math harder to think about. |
This comment has been minimized.
This comment has been minimized.
Just discovered this, don't know how I hadn't seen your comment yet -- super cool! FYI if you dig broadcasting, the whole point of Ugh if only that played better with windows. |
This comment has been minimized.
This comment has been minimized.
So this is how |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
I had way too much fun with this. I'm starting to appreciate broadcasting, so I took a stab at how you might solve these two specific problems with new functions that attempt to mimic basic broadcasting for subtraction and addition (%-% and %+%).
The first bit it me exploring what R does natively, then I create the functions, then test a few things!
I am completely aware the functions would need to be dramatically improved, but I think its kind of neat.
FYI - the most important bit is the Pairwise Distance chunk down at the bottom so you may want to just skip to that first.